#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[100007];
queue<int> q;
int deg[100007];
int cost[100007];
bool vis[100007];
int n, k;
bool check(int mid) {
  int i, j, koyta = 0;
  for (i = 1; i <= n; i++) {
    cost[i] = 0;
    vis[i] = false;
    deg[i] = v[i].size();
    if (deg[i] > 2) {
      koyta++;
    } else if (deg[i] == 1) {
      q.push(i);
    }
  }
  int tot = n;
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    pair<int, int> p;
    for (i = 0; i < v[s].size(); i++) {
      p = v[s][i];
      if (!vis[p.first]) {
        break;
      }
    }
    if (cost[s] + p.second > mid) continue;
    vis[s] = true;
    cost[p.first] = max(cost[p.first], cost[s] + p.second);
    if (deg[p.first] == 3) {
      koyta--;
    }
    deg[p.first]--;
    tot--;
    if (deg[p.first] == 1) q.push(p.first);
  }
  if (koyta == 0 && tot <= k) return true;
  return false;
}
int main() {
  int i, j, l, x, y, c;
  cin >> n >> k;
  for (i = 0; i < n - 1; i++) {
    cin >> x >> y >> c;
    v[x].push_back({y, c});
    v[y].push_back({x, c});
  }
  int low = 0, high = n * 10000 + 7, mid, ans;
  while (low <= high) {
    mid = (low + high) / 2;
    if (check(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
