#include <bits/stdc++.h>
using namespace std;
const int oo = 2e9;
const int MOD = 1000000007;
const int N = 200010;
const int LOGN = 18;
int n, k, at, mx, par[N], tf[N], ts[N], mxd[N];
bool inList[N];
vector<int> lst;
vector<pair<int, int> > g[N];
void dfs(int u, int p, int dist) {
  if (dist > mx) {
    mx = dist;
    at = u;
  }
  tf[u] = dist;
  par[u] = p;
  for (int i = 0; i < g[u].size(); ++i) {
    pair<int, int> v = g[u][i];
    if (v.first == p) continue;
    dfs(v.first, u, dist + v.second);
  }
}
void maxDFS(int u, int p) {
  mxd[u] = 0;
  for (int i = 0; i < g[u].size(); ++i) {
    pair<int, int> v = g[u][i];
    if (p == v.first) continue;
    if (inList[v.first]) continue;
    maxDFS(v.first, u);
    mxd[u] = max(mxd[u], v.second + mxd[v.first]);
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n - 1; ++i) {
    int u, v, d;
    scanf("%d%d%d", &u, &v, &d);
    g[u].push_back(make_pair(v, d));
    g[v].push_back(make_pair(u, d));
  }
  mx = at = -1;
  dfs(1, 1, 0);
  int root = at;
  mx = at = -1;
  dfs(root, -1, 0);
  while (at != -1) {
    inList[at] = 1;
    lst.push_back(at);
    at = par[at];
  }
  for (int i = 0; i < lst.size(); ++i) {
    maxDFS(lst[i], -1);
  }
  for (int i = 1; i <= n; ++i) ts[i] = mx - tf[i];
  deque<int> q;
  int idx = 0;
  int fn = oo;
  for (int i = 0; i < lst.size(); ++i) {
    while (idx < i + k && idx < lst.size()) {
      while (q.size() && mxd[q.front()] < mxd[lst[idx]]) q.pop_front();
      q.push_back(lst[idx]);
      idx++;
    }
    int last = lst[min((int)lst.size() - 1, i + k - 1)];
    int frist = lst[i];
    int hi = max(mxd[q.front()], ts[frist]);
    hi = max(hi, tf[last]);
    fn = min(fn, hi);
    if (q.size() && q.front() == lst[i]) q.pop_front();
  }
  cout << fn << endl;
  return 0;
}
