#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, k, head[100005], nxt[100005 << 1], to[100005 << 1], val[100005 << 1],
    cnt = 0;
void add(int x, int y, int z) {
  cnt++;
  to[cnt] = y, val[cnt] = z;
  nxt[cnt] = head[x], head[x] = cnt;
}
bool is[100005];
int dis[100005], fa[100005];
void dfs(int x, int f) {
  for (int p = head[x]; p; p = nxt[p]) {
    int v = to[p];
    if (v == f || is[v]) continue;
    dis[v] = dis[x] + val[p];
    fa[v] = x;
    dfs(v, x);
  }
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n - 1; i++) {
    int x = read(), y = read(), z = read();
    add(x, y, z);
    add(y, x, z);
  }
  int k1 = 1, k2 = 0;
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    if (dis[i] > dis[k1]) k1 = i;
  dis[k1] = 0, fa[k1] = 0;
  dfs(k1, 0);
  k2 = k1;
  for (int i = 1; i <= n; i++)
    if (dis[i] > dis[k2]) k2 = i;
  int ans = INT_MAX;
  int j = k2;
  for (int i = 1; i < k && fa[j]; i++) j = fa[j];
  int t = k2;
  while (j) ans = min(ans, max(dis[j], dis[k2] - dis[t])), j = fa[j], t = fa[t];
  for (int i = k2; i; i = fa[i]) is[i] = 1;
  for (int i = k2; i; i = fa[i]) dis[i] = 0, dfs(i, 0);
  for (int i = 1; i <= n; i++) ans = max(ans, dis[i]);
  cout << ans << endl;
  return 0;
}
