#include<iostream> #include<cstdio> #include<vector> #define MAX_N 500000 #define INF 0x3f3f3f3f usingnamespace std; int n, s, l, r; vector <int> G[MAX_N+5], E[MAX_N+5]; int d[MAX_N+5], f[MAX_N+5]; bool mrk[MAX_N+5]; voidadd(int u, int v, int c){G[u].push_back(v), E[u].push_back(c);} voidinit(){scanf("%d%d", &n, &s); for (int i = 1, u, v, c; i < n; i++) scanf("%d%d%d", &u, &v, &c), add(u, v, c), add(v, u, c);} voidDFS(int u){ for (int i = 0; i < (int)G[u].size(); i++) if (G[u][i]^f[u] && !mrk[G[u][i]]) d[G[u][i]] = d[u]+E[u][i], f[G[u][i]] = u, DFS(G[u][i]); } voidgetD(){ f[1] = d[1] = 0, DFS(1); for (int i = 1; i <= n; i++) if (!r || d[i] > d[r]) r = i; f[r] = d[r] = 0, DFS(r); for (int i = 1; i <= n; i++) if (!l || d[i] > d[l]) l = i; for (int i = l; i; i = f[i]) mrk[i] = true; } voidsol(){ int ans = INF; for (int i = l, j = l; i; i = f[i]) { while (f[j] && d[i]-d[f[j]] <= s) j = f[j]; ans = min(ans, max(d[l]-d[i], d[j])); } for (int i = l; i; i = f[i]) d[i] = 0, DFS(i); for (int i = 1; i <= n; i++) ans = max(ans, d[i]); printf("%d", ans); } intmain(){init(), getD(), sol(); return0;}