#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e5 + 50;
vector<pair<long long, long long> > v[MAX];
set<pair<long long, pair<long long, long long> > > sd1[MAX];
long long fad1[MAX];
void dfs1(long long x, long long p) {
  long long a, b, c, i, j;
  fad1[x] = 0;
  sd1[x].clear();
  for (auto z : v[x]) {
    if (z.first == p) continue;
    dfs1(z.first, x);
    fad1[x] = max(fad1[x], (fad1[z.first] + z.second));
    sd1[x].insert({(fad1[z.first] + z.second), {z.second, z.first}});
  }
}
long long find1() {
  long long a, b, c, i, j;
  a = 1;
  pair<long long, pair<long long, long long> > X, Y;
  while (true) {
    if (sd1[a].size() == 0) break;
    auto z = sd1[a].end();
    --z;
    X = *z;
    if (z == sd1[a].begin())
      c = 0;
    else {
      --z;
      c = (*z).first;
    }
    if (X.first > max((X.first - X.second.first), (c + X.second.first))) {
      sd1[X.second.second].insert({(c + X.second.first), {X.second.first, a}});
      a = X.second.second;
    } else
      break;
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(25);
  long long a, b, c, d, e, f, i, j, k;
  long long N, K;
  cin >> N >> K;
  for (i = 0; i < N - 1; i++) {
    cin >> a >> b >> c;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  if (N == 1) {
    cout << 0;
    return 0;
  }
  dfs1(1, -1);
  a = find1();
  dfs1(a, -1);
  set<pair<long long, long long> > ss;
  auto z = sd1[a].end();
  z--;
  ss.insert({(*z).first, (*z).second.second});
  sd1[a].erase(z);
  if (sd1[a].size() > 0) {
    z = sd1[a].end();
    z--;
    ss.insert({(*z).first, (*z).second.second});
    sd1[a].erase(z);
  }
  set<long long> rem;
  for (auto z1 : sd1[a]) rem.insert(z1.first);
  K--;
  while (K > 0 && (!ss.empty())) {
    auto Z = *(--ss.end());
    ss.erase(Z);
    a = Z.second;
    if (sd1[a].size() > 0) {
      auto z1 = --sd1[a].end();
      ss.insert({(*z1).first, (*z1).second.second});
      sd1[a].erase(z1);
    }
    for (auto z2 : sd1[a]) {
      rem.insert(z2.first);
    }
    K--;
  }
  long long ans = 0;
  if (!rem.empty()) (ans = max(ans, *(--rem.end())));
  if (!ss.empty()) (ans = max(ans, (*(--ss.end())).first));
  cout << ans;
  return 0;
}
