#include using namespace std; const int BS = 128; const int MAXN = 100005; int Val[MAXN], B[MAXN], E[MAXN], Sort[MAXN], Dist[MAXN], Size[MAXN]; int now = 0, n; long long total_paths = 0; vector> G[MAXN]; void dfs(int node) { now += 1; Val[now] = Dist[node]; B[node] = now; Size[node] = 1; for(auto vec : G[node]) { if(!B[vec.first]) { Dist[vec.first] = Dist[node] + vec.second; dfs(vec.first); Size[node] += Size[vec.first]; } } E[node] = now; total_paths += Size[node] - 1; } void preprocess() { for(int i = 1; i <= now; ++i) { Sort[i] = Val[i]; } for(int i = 1; i <= now; i += BS) { sort(Sort + i, Sort + min(i + BS, n + 1)); } } int query(int l, int r, int k) { int ans = 0; for(int i = l; i <= r;) { if(i % BS == 0 && min(n, i + BS - 1) < r) { ans += upper_bound(Sort + i, Sort + min(i + BS, n + 1), k) - Sort - i; i += BS; } else { if(Val[i] <= k) ans += 1; i += 1; } } return ans; } long long solve(int k) { long long ans = 0; for(int i = 1; i <= n; ++i) { if(k + Dist[i] < 0) ans += E[i] - B[i]; else ans += query(B[i] + 1, E[i], k + Dist[i]); } return ans; } void read(int &a) { char c; for(c = getchar(); !isdigit(c); c = getchar()); for(a = 0; isdigit(c); c = getchar()) a = a * 10 + c - '0'; } int main() { int a, b, c; long long m; cin >> n >> m; for(int i = 2; i <= n; ++i) { read(a); read(b); read(c); G[a].push_back({b, c}); G[b].push_back({a, c}); } dfs(1); preprocess(); if(m > total_paths) { cout << -1; return 0; } int ans = -1; for(int step = (1 << 29); step; step >>= 1) { if(solve(step + ans) < m) ans += step; } cout << ans + 1; return 0; }