#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
vector<pair<int, int> > e[N], dia;
int d[N], fa[N], vis[N], mx, n, K, i, j, x, y, z, id, a[N], tot, res = 1e9 + 7,
                                                                 q[N], t, w;
int init(int x) {
  fa[x] = d[x] = mx = 0;
  return x;
}
void dfs(int x) {
  mx = max(mx, d[x]);
  for (auto& y : e[x])
    if (!vis[y.first] && y.first != fa[x])
      fa[y.first] = x, d[y.first] = y.second + d[x], dfs(y.first);
}
int main() {
  for (scanf("%d%d", &n, &K), i = 1; i < n; ++i)
    scanf("%d%d%d", &x, &y, &z), e[x].push_back({y, z}), e[y].push_back({x, z});
  for (dfs(init(1)), id = 0, i = 1; i <= n; ++i)
    if (d[i] >= d[id]) id = i;
  for (dfs(init(id)), id = 0, i = 1; i <= n; ++i)
    if (d[i] >= d[id]) id = i;
  for (; id; id = fa[id]) dia.push_back({id, d[id]}), vis[id] = 1;
  reverse(dia.begin(), dia.end());
  K = min(K, (int)dia.size());
  for (auto& x : dia) dfs(init(x.first)), a[tot++] = mx;
  for (i = j = t = w = 0; i + K <= tot; ++i) {
    while (j < i + K) {
      while (t < w && a[q[w - 1]] < a[j]) w--;
      q[w++] = j++;
    }
    while (q[t] < i) t++;
    res = min(res, max({dia[i].second,
                        dia[tot - 1].second - dia[i + K - 1].second, a[q[t]]}));
  }
  printf("%d\n", res);
}
