#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
vector<vector<int> > dis;
void dfs(int u, int p, int d, int &fn, int &fd) {
  if (d > fd) {
    fd = d;
    fn = u;
  }
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    int e = dis[u][i];
    if (v == p) continue;
    dfs(v, u, d + e, fn, fd);
  }
}
bool path(int u, int p, int dest, vector<int> &P, vector<int> &PP) {
  if (u == dest) {
    PP.push_back(u);
    return true;
  }
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    int e = dis[u][i];
    if (v == p) continue;
    if (path(v, u, dest, P, PP)) {
      P.push_back(e);
      PP.push_back(u);
      return true;
    }
  }
  return false;
}
void dfs(int u, int p, int d, int &fn, int &fd, vector<int> &OK) {
  if (d > fd) {
    fd = d;
    fn = u;
  }
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i];
    int e = dis[u][i];
    if (v == p) continue;
    dfs(v, u, d + (OK[u] && OK[v] ? 0 : e), fn, fd, OK);
  }
}
int main(int argc, char *argv[]) {
  int N, K;
  cin >> N >> K;
  adj = vector<vector<int> >(N), dis = vector<vector<int> >(N);
  for (int i = 0; i < N - 1; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    adj[u].push_back(v);
    dis[u].push_back(c);
    adj[v].push_back(u);
    dis[v].push_back(c);
  }
  int fn = -1, fd = -1;
  dfs(0, -1, 0, fn, fd);
  int fn0 = fn;
  fd = -1;
  dfs(fn0, -1, 0, fn, fd);
  int fn1 = fn;
  vector<int> P, PP;
  path(fn0, -1, fn1, P, PP);
  vector<int> OK(N);
  if (K >= PP.size()) {
    for (int p : PP) OK[p] = 1;
  } else {
    vector<int> PS(P.size());
    for (int i = 0; i < P.size(); ++i) {
      PS[i] = P[i];
      if (i) PS[i] += PS[i - 1];
    }
    int b = 2000000000, bid = -1;
    int s = 0;
    for (int i = PP.size() - 1; i >= (K - 1); --i) {
      int j = i - (K - 1);
      int t = max(j == 0 ? 0 : PS[j - 1], s);
      if (t < b) {
        b = min(b, t);
        bid = j;
      }
      if (i == 0) break;
      s += P[i - 1];
    }
    for (int j = bid; j < bid + K; ++j) {
      OK[PP[j]] = 1;
    }
  }
  fd = -1;
  fn = -1;
  for (int i = 0; i < N; ++i) {
    if (OK[i]) {
      dfs(i, -1, 0, fn, fd, OK);
      break;
    }
  }
  cout << fd << endl;
}
