#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 69, base = 1024 * 1024, mod = 1e9 + 7;
long long odl[N], n, k, wynik;
vector<long long> vek, V;
vector<pair<int, int> > G[N];
void dfs1(int v, int oj) {
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i].first;
    if (u == oj) continue;
    odl[u] = odl[v] + G[v][i].second;
    dfs1(u, v);
  }
}
void dfs2(int v, int oj) {
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i].first;
    if (u == oj) {
      G[v].erase(G[v].begin() + i);
      i--;
      continue;
    }
    dfs2(u, v);
    odl[v] = max(odl[v], odl[u] + G[v][i].second);
  }
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i].first;
    if (odl[u] + G[v][i].second > odl[G[v][0].first] + G[v][0].second)
      swap(G[v][0], G[v][i]);
  }
}
void dfs3(int v, int gl) {
  vek.push_back(odl[v]);
  for (int i = 0; i < G[v].size(); i++) {
    if (i == 0) continue;
    int u = G[v][i].first;
    wynik = max(wynik, odl[u] + G[v][i].second);
  }
  if (gl < k)
    V.push_back(odl[v]);
  else {
    long long pom = odl[v];
    pom = max(pom, vek[0] - vek[gl - k + 1]);
    V.push_back(pom);
  }
  if (G[v].empty()) return;
  dfs3(G[v][0].first, gl + 1);
}
int32_t main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back(make_pair(b, c));
    G[b].push_back(make_pair(a, c));
  }
  dfs1(1, 0);
  int zap = 1;
  for (int i = 1; i <= n; i++)
    if (odl[i] > odl[zap]) zap = i;
  for (int i = 1; i <= n; i++) odl[i] = 0;
  dfs2(zap, 0);
  for (int i = 1; i <= n; i++) {
  }
  dfs3(zap, 0);
  sort(V.begin(), V.end());
  wynik = max(wynik, V[0]);
  cout << wynik << "\n";
}
