#include <bits/stdc++.h>
using namespace std;
int n, k, hea[100005], cnt, uu, vv, ww, dis[100005], fa[100005], len[100005],
    mxlen, d[100005];
bool vis[100005];
vector<int> zj, jl;
struct Edge {
  int too, nxt, val;
} edge[200005];
void add_edge(int fro, int too, int val) {
  edge[++cnt].nxt = hea[fro];
  edge[cnt].too = too;
  edge[cnt].val = val;
  hea[fro] = cnt;
}
void dfs(int x, int f) {
  fa[x] = f;
  mxlen = max(mxlen, dis[x]);
  for (int i = hea[x]; i; i = edge[i].nxt) {
    int t = edge[i].too;
    if (t != f && !vis[t]) {
      dis[t] = dis[x] + edge[i].val;
      dfs(t, x);
    }
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    scanf("%d %d %d", &uu, &vv, &ww);
    add_edge(uu, vv, ww);
    add_edge(vv, uu, ww);
  }
  dfs(1, 0);
  int st = 1;
  for (int i = 1; i <= n; i++)
    if (dis[i] > dis[st]) st = i;
  dis[st] = 0;
  dfs(st, 0);
  st = 1;
  for (int i = 1; i <= n; i++)
    if (dis[i] > dis[st]) st = i;
  for (; st; st = fa[st]) zj.push_back(st);
  reverse(zj.begin(), zj.end());
  for (int i = 0; i < zj.size(); i++) {
    jl.push_back(dis[zj[i]]);
    vis[zj[i]] = true;
  }
  for (int i = 0; i < zj.size(); i++) {
    int x = zj[i];
    dis[x] = mxlen = 0;
    dfs(x, 0);
    len[i] = mxlen;
  }
  k = min(k, (int)zj.size());
  mxlen = 0x3f3f3f3f;
  int ll = 1, rr = 0, j = 0;
  for (int i = 0; i <= zj.size() - k; i++) {
    while (j <= i + k - 1) {
      while (ll <= rr && len[d[rr]] <= len[j]) rr--;
      d[++rr] = j;
      j++;
    }
    while (d[ll] < i) ll++;
    mxlen = min(mxlen,
                max(max(len[d[ll]], jl[i]), jl[zj.size() - 1] - jl[i + k - 1]));
  }
  cout << mxlen << endl;
  return 0;
}
