#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int n, m, mxd;
struct edge {
  int to, nxt, w;
} e[300005 << 1];
int tot, head[300005];
inline void adde(int u, int v, int w) {
  e[++tot] = (edge){v, head[u], w};
  head[u] = tot;
}
int s, t, k;
int fa[300005], d[300005], dis[300005], frm[300005], cnt;
int dep[300005], mx[300005], top[300005];
bool vis[300005];
void dfs(int u, int pa) {
  fa[u] = pa;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pa) continue;
    dis[v] = dis[u] + e[i].w;
    frm[v] = i;
    dfs(v, u);
  }
}
void dfs2(int u, int p, int pa) {
  mx[p] = max(mx[p], dep[u]);
  top[u] = p;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pa || vis[v]) continue;
    dep[v] = dep[u] + e[i].w;
    dfs2(v, p, u);
  }
}
void find() {
  s = t = 1, dis[1] = 0;
  dfs(1, 0);
  for (register int i = (1); i <= (n); ++i)
    if (dis[i] > dis[t]) t = i;
  dis[t] = fa[t] = 0;
  dfs(t, 0);
  for (register int i = (1); i <= (n); ++i)
    if (dis[i] > dis[s]) s = i;
  int u = s;
  cnt = 0;
  while (fa[u]) d[++cnt] = u, vis[u] = 1, u = fa[u];
  d[++cnt] = t;
  vis[t] = 1;
}
signed main() {
  n = read(), k = read();
  tot = 1;
  for (register int i = (2); i <= (n); ++i) {
    int u = read(), v = read(), w = read();
    adde(u, v, w);
    adde(v, u, w);
  }
  find();
  for (register int i = (1); i <= (cnt); ++i)
    dfs2(d[i], i, 0), mxd = max(mxd, mx[i]);
  int r = 1, res = 2147483647;
  for (register int l = (1); l <= (cnt); ++l) {
    while (r < cnt && r - l + 2 <= k) r++;
    res = min(res, max(mxd, max(dis[d[r]], dis[s] - dis[d[l]])));
  }
  cout << res;
  return 0;
}
