#include <bits/stdc++.h>
const int N = 105000;
struct node {
  int v, next, dis;
} edge[N * 2];
int cnt;
int head[N];
int dp1[N];
int dp2[N];
int dp[N];
int q[N];
int ss[N];
int root, ed;
int big, l;
int b[N];
int vis[N];
int max(int a, int b) {
  if (a > b) return a;
  return b;
}
int min(int a, int b) {
  if (a < b) return a;
  return b;
}
void add(int u, int v, int dis) {
  cnt++;
  edge[cnt].v = v;
  edge[cnt].dis = dis;
  edge[cnt].next = head[u];
  head[u] = cnt;
}
void dfs_1(int k, int fa, int now) {
  int i;
  if (now > big) {
    big = now;
    root = k;
  }
  for (i = head[k]; i != -1; i = edge[i].next) {
    int v = edge[i].v;
    int d = edge[i].dis;
    if (v != fa) {
      dfs_1(v, k, now + d);
    }
  }
}
void dfs_2(int k, int fa, int now) {
  int i;
  if (now > big) {
    big = now;
    ed = k;
  }
  for (i = head[k]; i != -1; i = edge[i].next) {
    int v = edge[i].v;
    int d = edge[i].dis;
    if (v != fa) {
      dfs_2(v, k, now + d);
    }
  }
}
void dfs_3(int k, int fa) {
  int i;
  if (k == ed) {
    vis[k] = 1;
    l++;
    b[l] = k;
  }
  for (i = head[k]; i != -1; i = edge[i].next) {
    int v = edge[i].v;
    int d = edge[i].dis;
    if (v != fa) {
      dfs_3(v, k);
      if (vis[v] == 0) {
        dp[k] = max(dp[k], dp[v] + d);
      }
      if (vis[v] == 1) {
        vis[k] = 1;
        l++;
        b[l] = k;
      }
      dp1[k] = max(dp1[k], dp1[v] + d);
    }
  }
}
void dfs_4(int k, int fa) {
  int i;
  for (i = head[k]; i != -1; i = edge[i].next) {
    int v = edge[i].v;
    int d = edge[i].dis;
    if (v != fa) {
      dfs_4(v, k);
      dp2[k] = max(dp2[k], dp2[v] + d);
    }
  }
}
int main() {
  int n, k, i, x, y, d, front, tail, ans, now;
  cnt = 0;
  memset(head, -1, sizeof(head));
  scanf("%d %d", &n, &k);
  for (i = 1; i < n; i++) {
    scanf("%d %d %d", &x, &y, &d);
    add(x, y, d);
    add(y, x, d);
  }
  root = 0;
  big = 0;
  ed = 0;
  l = 0;
  memset(b, 0, sizeof(b));
  memset(dp, 0, sizeof(dp));
  memset(dp1, 0, sizeof(dp1));
  memset(dp2, 0, sizeof(dp2));
  memset(vis, 0, sizeof(vis));
  dfs_1(1, -1, 0);
  big = 0;
  dfs_2(root, -1, 0);
  dfs_3(root, -1);
  dfs_4(ed, -1);
  front = 0;
  tail = 0;
  q[0] = 0;
  ss[0] = 0;
  ans = 0x3f3f3f3f;
  for (i = 1; i <= l; i++) {
    while (i - ss[front] > k) front++;
    now = max(max(dp1[b[max(i - k + 1, 1)]], dp2[b[i]]), q[front]);
    ans = min(now, ans);
    now = dp[b[i]];
    while (tail >= front && now > q[tail]) tail--;
    tail++;
    q[tail] = now;
    ss[tail] = i;
  }
  printf("%d\n", ans);
}
