#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, k, dep[maxn], maxd, U, V, fr[maxn], pref[maxn], suff[maxn];
vector<pair<int, int>> g[maxn];
void dfs(int v) {
  for (auto e : g[v])
    if (dep[e.first] == 0) dep[e.first] = dep[v] + e.second, dfs(e.first);
}
void upd(int v) {
  for (int u = 0; u < n; u++)
    if (dep[u] > maxd) U = u, V = v, maxd = dep[u];
}
vector<int> cur, path, curr, pathh;
void dfs1(int v, int p) {
  cur.push_back(v);
  if (v == V) path = cur, pathh = curr;
  for (auto e : g[v])
    if (e.first != p)
      curr.push_back(e.second), dfs1(e.first, v), curr.pop_back();
  cur.pop_back();
}
void dfs2(int v, int st) {
  fr[st] = max(fr[st], dep[v]);
  for (auto e : g[v])
    if (dep[e.first] == -1) dep[e.first] = dep[v] + e.second, dfs2(e.first, st);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs(0);
  upd(0);
  memset(dep, 0, sizeof(dep));
  dfs(U);
  upd(U);
  dfs1(U, -1);
  memset(dep, -1, sizeof(dep));
  for (int v : path) dep[v] = 0;
  for (int v : path) dfs2(v, v);
  for (int i = 0; i < (int)((path).size()); i++)
    pref[i] = max(i ? pref[i - 1] + pathh[i - 1] : 0, fr[path[i]]);
  for (int i = (int)((path).size()) - 1; i >= 0; i--)
    suff[i] = max(i != (int)((path).size()) - 1 ? suff[i + 1] + pathh[i] : 0,
                  fr[path[i]]);
  k = min(k, (int)((path).size()));
  multiset<int> window;
  int ans = INT_MAX;
  for (int i = 0; i < k - 1; i++) window.insert(fr[path[i]]);
  for (int i = k - 1; i < (int)((path).size()); i++) {
    window.insert(fr[path[i]]);
    ans = min(ans, max({pref[i - k + 1], *window.rbegin(), suff[i]}));
    window.erase(fr[path[i - k + 1]]);
  }
  cout << ans;
}
