#include <bits/stdc++.h>
const int MAX_N = 1e5 + 5;
const int INF = 1e9 + 7;
const int STEP = 1 << 30;
int n, k, ans = INF, cnt[MAX_N], max[MAX_N];
std::bitset<MAX_N> chk;
std::vector<std::pair<int, int>> adj[MAX_N];
bool check(int dist) {
  std::queue<int> q;
  for (int i = 1; i <= n; ++i)
    if ((cnt[i] = adj[i].size()) == 1) q.push(i);
  memset(max, 0x00, sizeof(max));
  for (int i = 1; i <= n; ++i) chk.set(i);
  for (; !q.empty(); q.pop()) {
    int cur = q.front();
    for (auto tar : adj[cur])
      if (cnt[tar.first] && max[cur] + tar.second <= dist) {
        chk.reset(cur);
        --cnt[cur];
        max[tar.first] = std::max(max[tar.first], max[cur] + tar.second);
        if (--cnt[tar.first] == 1) q.push(tar.first);
        break;
      }
  }
  if (chk.count() > k) return false;
  for (int i = 1; i <= n; ++i)
    if (chk.test(i) && cnt[i] > 2) return false;
  return true;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
  }
  for (int step = STEP; step; step >>= 1)
    if (check(ans - step)) ans -= step;
  printf("%d\n", std::max(0, ans));
  return 0;
}
