#include <bits/stdc++.h>
using namespace std;
long long n, k, re, u, v, d;
set<pair<long long, long long> > edge[100059], sett;
set<pair<long long, long long> >::iterator it;
void solve() {
  while (n > k || sett.size() > 2) {
    n--;
    it = sett.begin();
    re = (*it).first;
    u = (*it).second;
    sett.erase(it);
    it = edge[u].begin();
    v = (*it).second;
    d = (*it).first;
    edge[v].erase(pair<long long, long long>(d, u));
    if (edge[v].size() == 1)
      sett.insert(pair<long long, long long>(re + (*edge[v].begin()).first, v));
  }
  cout << re;
}
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%lld%lld%lld", &u, &v, &d);
    edge[u].insert(pair<long long, long long>(d, v));
    edge[v].insert(pair<long long, long long>(d, u));
  }
  for (int i = 1; i <= n; i++) {
    if (edge[i].size() == 1)
      sett.insert(pair<long long, long long>((*edge[i].begin()).first, i));
  }
  solve();
}
