#include <bits/stdc++.h>
using namespace std;
struct st {
  int x, y, z, ne;
} edge[200010];
int n, m, x, y, z, I;
int h[100010], f[100010], deep[100010], a[100010];
void makeedge(int x, int y, int z) {
  I++;
  edge[I].x = x;
  edge[I].y = y;
  edge[I].z = z;
  edge[I].ne = h[x];
  h[x] = I;
}
void dfs(int x) {
  int p = h[x];
  while (p) {
    if (f[edge[p].y] == 0) {
      if (deep[edge[p].y] == -1) {
        deep[edge[p].y] = deep[x] + edge[p].z;
      }
      f[edge[p].y] = x;
      dfs(edge[p].y);
    }
    p = edge[p].ne;
  }
}
int main() {
  I = 0;
  memset(h, 0, sizeof(h));
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    makeedge(x, y, z);
    makeedge(y, x, z);
  }
  memset(f, 0, sizeof(f));
  f[1] = -1;
  memset(deep, -1, sizeof(deep));
  deep[1] = 0;
  dfs(1);
  x = 1;
  for (int i = 1; i <= n; i++) {
    if (deep[i] > deep[x]) x = i;
  }
  memset(f, 0, sizeof(f));
  f[x] = -1;
  memset(deep, -1, sizeof(deep));
  deep[x] = 0;
  dfs(x);
  y = x;
  for (int i = 1; i <= n; i++)
    if (deep[i] > deep[y]) y = i;
  int l = 0;
  while (1) {
    a[++l] = y;
    y = f[y];
    if (y == -1) break;
  }
  int LL = deep[a[1]];
  int L, R, ans;
  if (l <= m) {
    L = 1;
    R = l;
  } else {
    ans = 1;
    for (int i = 2; i <= l - m + 1; i++) {
      if (max(deep[a[i + m - 1]], LL - deep[a[i]]) <
          max(deep[a[ans + m - 1]], LL - deep[a[ans]]))
        ans = i;
    }
    L = ans;
    R = ans + m - 1;
  }
  memset(deep, -1, sizeof(deep));
  memset(f, 0, sizeof(f));
  for (int i = L; i <= R; i++) deep[a[i]] = 0;
  f[a[L]] = -1;
  dfs(a[L]);
  ans = 0;
  for (int i = 1; i <= n; i++)
    if (deep[i] > ans) ans = deep[i];
  printf("%d\n", ans);
}
