#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > head[100007], s;
int main() {
  int n, m, u, v, w, ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    head[u].insert(make_pair(v, w));
    head[v].insert(make_pair(u, w));
  }
  for (int i = 1; i <= n; i++)
    if (head[i].size() == 1) {
      int dis = (*head[i].begin()).second;
      s.insert(make_pair(dis, i));
    }
  while (n > m || s.size() > 2) {
    ans = (*s.begin()).first;
    int curr = (*s.begin()).second;
    s.erase(s.begin());
    int next = (*head[curr].begin()).first;
    head[next].erase(head[next].lower_bound(make_pair(curr, 0)));
    --n;
    if (head[next].size() == 1) {
      int dis = (*head[next].begin()).second;
      s.insert(make_pair(ans + dis, next));
    }
  }
  printf("%d", ans);
}
