#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  vector<vector<pair<int, int>>> G(N);
  for (int i = 0; i + 1 < N; ++i) {
    int U, V, D;
    cin >> U >> V >> D;
    G[U - 1].emplace_back(D, V - 1);
    G[V - 1].emplace_back(D, U - 1);
  }
  int lb = -1, ub = 0x3f3f3f3f;
  while (ub - lb > 1) {
    auto ok = [&](int lim) {
      vector<int> hei(N);
      vector<int> cov(N);
      vector<int> deg(N);
      queue<int> que;
      int ngcnt = 0;
      for (int i = 0; i < N; ++i) {
        deg[i] = G[i].size();
        if (deg[i] == 1) {
          que.emplace(i);
        } else if (deg[i] > 2) {
          ++ngcnt;
        }
      }
      int cnt = N;
      while (que.size()) {
        int u = que.front();
        que.pop();
        pair<int, int> s;
        for (const auto &e : G[u]) {
          if (!cov[e.second]) {
            s = e;
            break;
          }
        }
        if (hei[u] + s.first > lim) continue;
        hei[s.second] = max(hei[s.second], hei[u] + s.first);
        cov[u] = 1;
        --cnt;
        --deg[s.second];
        if (deg[s.second] == 2) --ngcnt;
        if (deg[s.second] == 1) que.emplace(s.second);
      }
      return ngcnt == 0 && cnt <= K;
    };
    int mb = lb + ub >> 1;
    (ok(mb) ? ub : lb) = mb;
  }
  cout << ub << endl;
  return 0;
}
