#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (S == T) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0, f = 1;
  char ch = gc();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = gc();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = gc();
  return x * f;
}
int n, s, fa[110010], a[110010], m, h[110010], num = 0, mx, rt, q[110010];
long long dis[110010], mxd[110010], ans = 1LL << 60;
bool mark[110010];
struct edge {
  int y, next, z;
} data[110010 << 1];
inline void dfs(int x) {
  if (dis[x] > dis[mx]) mx = x;
  for (int i = h[x]; i; i = data[i].next) {
    int y = data[i].y, z = data[i].z;
    if (y == fa[x]) continue;
    fa[y] = x;
    dis[y] = dis[x] + z;
    dfs(y);
  }
}
inline void dfs2(int x) {
  if (mxd[x] > mxd[mx]) mx = x;
  for (int i = h[x]; i; i = data[i].next) {
    int y = data[i].y, z = data[i].z;
    if (y == fa[x] || mark[y]) continue;
    mxd[y] = mxd[x] + z;
    dfs2(y);
  }
}
int main() {
  n = read();
  s = read();
  for (int i = 1; i < n; ++i) {
    int x = read(), y = read(), z = read();
    data[++num].y = y;
    data[num].next = h[x];
    h[x] = num;
    data[num].z = z;
    data[++num].y = x;
    data[num].next = h[y];
    h[y] = num;
    data[num].z = z;
  }
  mx = 1;
  dfs(1);
  rt = mx;
  fa[rt] = 0;
  dis[rt] = 0;
  dfs(rt);
  while (mx) a[++m] = mx, mark[mx] = 1, mx = fa[mx];
  for (int i = 1; i <= m; ++i) {
    mx = a[i];
    dfs2(a[i]);
    mxd[a[i]] = mxd[mx];
  }
  int l = 1, r = 1, h = 1, t = 0;
  for (; l <= m; ++l) {
    while (h <= t && q[h] < l) ++h;
    while (r <= m && r - l < s) {
      while (h <= t && mxd[a[r]] >= mxd[a[q[t]]]) --t;
      q[++t] = r;
      ++r;
    }
    ans =
        min(ans, max(max(dis[a[1]] - dis[a[l]], dis[a[r - 1]]), mxd[a[q[h]]]));
  }
  printf("%I64d\n", ans);
  return 0;
}
