#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=300010;
int n,m,cnt,len,r1,r2,h,t,ans;
int to[maxn<<1],next[maxn<<1],head[maxn],val[maxn<<1],fa[maxn],p[maxn],vis[maxn],f[maxn],dep[maxn],q[maxn],v[maxn];
void dfs(int x)
{
if(dep[x]>dep[r2]) r2=x;
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa[x]) fa[to[i]]=x,dep[to[i]]=dep[x]+val[i],dfs(to[i]);
}
inline void add(int a,int b,int c)
{
to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
}
int getv(int x)
{
int tmp=dep[x];
for(int i=head[x];i!=-1;i=next[i]) if(!vis[to[i]]&&to[i]!=fa[x]) tmp=max(tmp,getv(to[i]));
return tmp;
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<‘0‘||gc>‘9‘) {if(gc==‘-‘)f=-f; gc=getchar();}
while(gc>=‘0‘&&gc<=‘9‘) ret=ret*10+gc-‘0‘,gc=getchar();
return ret*f;
}
int main()
{
n=rd(),m=rd();
int i,j,a,b,c;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) a=rd(),b=rd(),c=rd(),add(a,b,c),add(b,a,c);
r2=1,dfs(1);
memset(dep,0,sizeof(dep)),memset(fa,0,sizeof(fa));
r1=r2,dfs(r1);
for(a=r2;a;p[++len]=a,vis[a]=1,a=fa[a]);
for(i=1;i<=len;i++) v[i]=getv(p[i])-dep[p[i]];
h=1,t=0,ans=1<<30;
for(i=j=1;i<=len;i++)
{
for(;j<i&&dep[p[j]]-dep[p[i]]>m;j++);
while(h<=t&&v[q[t]]<=v[i]) t--;
q[++t]=i;
while(h<=t&&q[h]<j) h++;
ans=min(ans,max(max(dep[p[i]]-dep[p[len]],dep[p[1]]-dep[p[j]]),v[q[h]]));
}
printf("%d",ans);
return 0;
}