#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 30;
vector<pair<int, int>> g[MAXN];
int sz[MAXN];
void dfs3(int u, int p) {
  assert(u);
  sz[u] = 0;
  for (auto e : g[u]) {
    int v = e.first;
    if (v != p) {
      dfs3(v, u);
      sz[u] = max(sz[u], sz[v] + e.second);
    }
  }
}
int dfs2(int u, int p, int m, int k) {
  assert(u);
  pair<int, int> mx = {0, 0};
  for (auto e : g[u]) {
    int v = e.first;
    if (v != p) {
      if (sz[v] + e.second > m && mx.first > m) return -2;
      mx = max(mx, {sz[v] + e.second, v});
    }
  }
  if (mx.first <= m) return -1;
  if (!k) return -2;
  return dfs2(mx.second, u, m, k - 1);
}
int dfs(int u, int p, int m, int k) {
  int mx = 0;
  for (auto e : g[u]) {
    int v = e.first;
    if (v != p) {
      int res = dfs(v, u, m, k);
      if (res < 0) return res;
      if (res + e.second > m) {
        dfs3(v, -1);
        int res2 = dfs2(v, -1, m, k - 1);
        assert(res2 < 0);
        return res2;
      }
      mx = max(mx, res + e.second);
    }
  }
  if (p == -1) return -1;
  return mx;
}
signed main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    g[u].push_back({v, d});
    g[v].push_back({u, d});
  }
  int l = -1, r = 1e9, m;
  while (l + 1 < r) {
    m = (l + r) / 2;
    if (dfs(1, -1, m, k) == -1)
      r = m;
    else
      l = m;
  }
  cout << r;
  return 0;
}
