#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
vector<pair<int, int> > g[maxn];
int d[maxn], par[maxn];
void dfs1(int u, int fa) {
  par[u] = fa;
  for (auto p : g[u]) {
    int v = p.first;
    if (v == fa) continue;
    d[v] = d[u] + p.second;
    dfs1(v, u);
  }
}
int a[maxn];
bool f[maxn];
int c[maxn];
void dfs(int u, int fa) {
  d[u] = 0;
  par[u] = fa;
  for (auto p : g[u]) {
    int v = p.first;
    if (v == fa) continue;
    dfs(v, u);
    c[v] = p.second;
    if (!f[v]) d[u] = max(d[u], d[v] + p.second);
  }
}
int l[maxn], r[maxn], t[maxn], tail = 1;
int main() {
  scanf("%d%d", &n, &k);
  int u, v, w;
  for (int i = 1; i <= n - 1; i++)
    scanf("%d%d%d", &u, &v, &w), g[u].push_back(pair<int, int>(v, w)),
        g[v].push_back(pair<int, int>(u, w));
  dfs1(1, -1);
  d[1] = 0;
  int rt = 1;
  for (int i = 1; i <= n; i++)
    if (d[i] > d[rt]) rt = i;
  d[rt] = 0;
  dfs1(rt, -1);
  int y = rt;
  for (int i = 1; i <= n; i++)
    if (d[i] > d[y]) y = i;
  int p = y;
  int m = 0;
  while (p > 0) a[++m] = p, p = par[p];
  dfs(rt, -1);
  for (int i = 1; i <= n; i++) l[i] = d[i];
  dfs(y, -1);
  for (int i = 1; i <= n; i++) r[i] = d[i];
  for (int i = 1; i <= m; i++) f[a[i]] = 1;
  dfs(rt, -1);
  tail = 1;
  int head = 1;
  int ans = 1e9;
  for (int i = 1; i <= m; i++) {
    while (tail > head && d[a[t[tail - 1]]] <= d[a[i]]) tail--;
    t[tail++] = i;
    int q = d[a[t[head]]];
    q = max(q, (i - k + 1 > 0 ? l[a[i - k + 1]] : 0));
    q = max(q, r[a[i]]);
    ans = min(ans, q);
    if (t[head] == i - k) head++;
  }
  printf("%d\n", ans);
  return 0;
}
