#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, k, o, hd[N], A, B, d[N], mx[N], fa[N], pre[N], head, tail;
struct data {
  int u, x;
} q[N];
struct Edge {
  int u, v, w, nt;
} E[N << 1];
void adde(int u, int v, int w) {
  E[o] = (Edge){u, v, w, hd[u]};
  hd[u] = o++;
  E[o] = (Edge){v, u, w, hd[v]};
  hd[v] = o++;
}
void dfs1(int u, int F) {
  if (d[u] > d[A]) A = u;
  for (int i = hd[u], v; ~i; i = E[i].nt) {
    if ((v = E[i].v) == F) continue;
    d[v] = d[u] + E[i].w;
    dfs1(v, u);
  }
}
void dfs2(int u, int F) {
  fa[u] = F;
  if (d[u] > d[B]) B = u;
  for (int i = hd[u], v; ~i; i = E[i].nt) {
    if ((v = E[i].v) == F) continue;
    d[v] = d[u] + E[i].w;
    pre[v] = E[i].w;
    dfs2(v, u);
    mx[u] = max(mx[v] + E[i].w, mx[u]);
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) hd[i] = -1;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    adde(u, v, w);
  }
  A = 1;
  dfs1(1, 0);
  for (int i = 1; i <= n; i++) d[i] = 0;
  B = A;
  dfs2(A, 0);
  int x = B, y = B, dB = 0;
  for (int u = B, P = 0; u; P = u, u = fa[u]) {
    mx[u] = 0;
    for (int i = hd[u]; ~i; i = E[i].nt) {
      int v = E[i].v;
      if (v == P || v == fa[u]) continue;
      mx[u] = max(mx[u], mx[v] + E[i].w);
    }
  }
  for (int i = 1;; x = fa[x], i++) {
    while (head < tail && mx[x] > q[tail].x) tail--;
    q[++tail] = (data){x, mx[x]};
    if (i == k || x == A) break;
  }
  int ans = 1e9;
  for (; x; x = fa[x], y = fa[y]) {
    ans = min(ans, max(max(d[x], dB), q[head + 1].x));
    if (head < tail && q[head + 1].u == y) head++;
    dB += pre[y];
    while (head < tail && q[tail].x < mx[fa[x]]) tail--;
    q[++tail] = (data){fa[x], mx[fa[x]]};
  }
  cout << ans << endl;
  return 0;
}
