#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
bool viz[N];
int rad[N], grad[N], dist[N];
vector<pair<int, int> > muchii[N];
int n, k;
map<pair<int, int>, int> d;
bool cool(int valm) {
  int st(0), dr(-1), noduri(n), nusir(0);
  for (int i = 1; i <= n; ++i) {
    viz[i] = false;
    dist[i] = 0;
    grad[i] = muchii[i].size();
    if (grad[i] >= 3) {
      ++nusir;
    }
    if (grad[i] == 1) {
      rad[++dr] = i;
    }
  }
  while (st <= dr) {
    int x = rad[st++];
    for (int i = 0; i < muchii[x].size(); ++i) {
      int nod(muchii[x][i].first), cost(muchii[x][i].second);
      if (!(viz[nod])) {
        if (dist[x] + cost <= valm) {
          dist[nod] = max(dist[nod], dist[x] + cost);
          viz[x] = true;
          --grad[nod];
          --noduri;
          if (grad[nod] == 2) {
            --nusir;
          }
          if (grad[nod] == 1) {
            rad[++dr] = nod;
          }
        }
      }
    }
  }
  return !nusir & (noduri <= k);
}
int cb() {
  int pas(1 << 29), r(-1);
  while (pas > 0) {
    if (!cool(r + pas)) {
      r += pas;
    }
    pas /= 2;
  }
  return r + 1;
}
int main() {
  int a, b, val;
  cin >> n >> k;
  for (int i = 1; i < n; ++i) {
    cin >> a >> b >> val;
    muchii[a].push_back(make_pair(b, val));
    muchii[b].push_back(make_pair(a, val));
  }
  cout << cb();
  return 0;
}
