#include #include #include #include using namespace std; const int MAXN = 100005; bool Viz[MAXN]; int D[MAXN], Parent[18][MAXN]; int n; vector> G[MAXN]; void dfs(int node) { Viz[node] = 1; for(auto vec : G[node]) { if(!Viz[vec.first]) { D[vec.first] = D[node] + vec.second; Parent[0][vec.first] = node; for(int i = 1; Parent[i - 1][vec.first]; ++i) Parent[i][vec.first] = Parent[i - 1][Parent[i - 1][vec.first]]; dfs(vec.first); } } } int Node[MAXN]; long long solve(int k) { for(int i = 1; i <= n; ++i) Node[i] = i; long long ans = 0; for(int i = 17; i >= 0; --i) { for(int node = 1; node <= n; ++node) if(Parent[i][Node[node]] && D[node] - D[Parent[i][Node[node]]] <= k) { ans += (1 << i); Node[node] = Parent[i][Node[node]]; } } return ans; } int main() { int a, b, c; long long m; cin >> n >> m; for(int i = 2; i <= n; ++i) { cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } dfs(1); int ans = -1; for(int step = (1 << 29); step; step >>= 1) { if(solve(step + ans) < m) ans += step; } if(ans > 1e9) ans = -2; cout << ans + 1; return 0; }