#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9 + 1e8 + 1234;
const long long linf = 8e18 + 9e17;
const int N = 1e5 + 7;
const int mod = 998244353;
int n, k, ans;
vector<pair<int, int> > mp[N];
int vis[N], dep[N], dis[N], fa[N];
vector<int> diameter;
void dfs1(int u, int Fa) {
  if (vis[u]) return;
  vis[u] = 1;
  for (int i = 0; i < mp[u].size(); ++i) {
    int v = mp[u][i].first, w = mp[u][i].second;
    if (v == Fa) continue;
    fa[v] = u;
    dis[v] = dis[u] + w;
    dfs1(v, u);
  }
}
void dfs2(int u, int f) {
  for (auto x : mp[u]) {
    if (vis[x.first] || x.first == f) continue;
    dfs2(x.first, u);
    dep[u] = max(dep[x.first] + x.second, dep[u]);
  }
}
int main(int argc, char const *argv[]) {
  while (~scanf("%d%d", &n, &k)) {
    for (int i = 0; i <= n; ++i) mp[i].clear();
    diameter.clear();
    for (int i = 1, u, v, w; i < n; ++i) {
      scanf("%d%d%d", &u, &v, &w);
      mp[u].push_back(make_pair(v, w));
      mp[v].push_back(make_pair(u, w));
    }
    int st, ed, tmax = -1;
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    dfs1(1, -1);
    for (int i = 1; i <= n; ++i) {
      if (tmax < dis[i]) {
        tmax = dis[i];
        st = i;
      }
    }
    memset(fa, 0, sizeof(fa));
    memset(vis, 0, sizeof(vis));
    memset(dis, 0, sizeof(dis));
    dfs1(st, -1);
    tmax = -1;
    for (int i = 1; i <= n; ++i) {
      if (tmax < dis[i]) {
        tmax = dis[i];
        ed = i;
      }
    }
    memset(vis, 0, sizeof(vis));
    for (int i = ed; i; i = fa[i]) {
      diameter.push_back(i);
      vis[i] = 1;
    }
    ans = inf;
    int h_max = 0, x = 0;
    memset(dep, 0, sizeof(dep));
    for (int i = 0; i < diameter.size(); ++i, x = 0) {
      if (i >= k) {
        x = dis[ed] - dis[diameter[i - k + 1]];
      }
      x = max(x, dis[diameter[i]]);
      dfs2(diameter[i], -1);
      h_max = max(h_max, dep[diameter[i]]);
      x = max(x, h_max);
      ans = min(ans, x);
    }
    printf("%d\n", ans);
  }
  return 0;
}
