#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
inline int read()
{
int s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
return s*w;
}
int n,m,head[N],cnt,maxn,d1[N],d2[N],ans,f[N];
struct node{
int to,net,u,val;
}e[N<<1];
multiset<int> s[N];
void add(int x,int y,int z)
{
e[++cnt].to=y;e[cnt].net=head[x];head[x]=cnt;e[cnt].val=z;
}
int dfs1(int x,int fa)
{
d1[x]=d2[x]=0;
for(int i=head[x];i;i=e[i].net)
{
int to=e[i].to;
if(to==fa)continue;
dfs1(to,x);
int t=d1[to]+e[i].val;
if(t>d1[x])
{
d2[x]=d1[x];d1[x]=t;
}else if(t>d2[x])
{
d2[x]=t;
}
}
maxn=max(maxn,d1[x]+d2[x]);
}
int dfs2(int x,int fa,int mid)
{
s[x].clear();
for(int i=head[x];i;i=e[i].net)
{
int to=e[i].to;
if(to==fa)continue;
dfs2(to,x,mid);
if(e[i].val+f[to]>=mid)ans++;
else s[x].insert(f[to]+e[i].val);
}
while(!s[x].empty())
{
multiset<int>::iterator it1=s[x].begin();
s[x].erase(it1);
multiset<int>::iterator it2=s[x].lower_bound(mid-*it1);
if(it2==s[x].end())
{
f[x]=max(f[x],*it1);
}
else
{
ans++;s[x].erase(it2);
}
}
}
bool check(int mid)
{
ans=0;memset(f,0,sizeof(f));
dfs2(1,0,mid);
if(ans>=m)return 1;
else return 0;
}
signed main()
{
n=read();m=read();
for(int i=1;i<n;i++)
{
int x=read(),y=read(),w=read();
add(x,y,w);add(y,x,w);
}
dfs1(1,0);
int l=0,r=maxn;
while(l<r)
{
int mid=(l+r+1)>>1;
if(check(mid))l=mid;
else r=mid-1;
}
cout<<l;
return 0;
}