#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 7;
const int inf = 0x3f3f3f3f;
int n, k;
int fa[MX];
long long MAX, dis[MX];
bool vis[MX];
vector<pair<int, int> > E[MX];
vector<int> point, Tree, v;
void dfs(int u, int f) {
  fa[u] = f;
  MAX = max(MAX, dis[u]);
  for (auto nw : E[u]) {
    int v = nw.first, w = nw.second;
    if (v == f || vis[v]) continue;
    dis[v] = dis[u] + w;
    dfs(v, u);
  }
}
void Tree_init() {
  dfs(1, -1);
  int st, ed;
  long long mx = -1;
  for (int i = 1; i <= n; i++) {
    if (dis[i] > mx) {
      mx = dis[i];
      st = i;
    }
  }
  memset(dis, 0, sizeof(dis));
  dfs(st, -1);
  mx = -1;
  for (int i = 1; i <= n; i++) {
    if (dis[i] > mx) {
      mx = dis[i];
      ed = i;
    }
  }
  for (int i = ed; i != -1; i = fa[i]) point.push_back(i);
  reverse(point.begin(), point.end());
  k = min(k, (int)point.size());
  for (auto x : point) {
    vis[x] = 1;
    v.push_back(dis[x]);
  }
  for (auto x : point) {
    MAX = 0;
    dis[x] = 0;
    dfs(x, -1);
    Tree.push_back(MAX);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int uu, vv, w;
    cin >> uu >> vv >> w;
    E[uu].push_back(make_pair(vv, w));
    E[vv].push_back(make_pair(uu, w));
  }
  Tree_init();
  deque<pair<long long, long long> > q;
  long long sz = point.size();
  long long ans = 1e18;
  for (int i = 0, j = 0; i <= sz - k; i++) {
    while (j <= i + k - 1) {
      while (!q.empty() && Tree[j] > q.back().first) q.pop_back();
      q.push_back(make_pair(Tree[j], j));
      j++;
    }
    while (q.front().second < i) q.pop_front();
    ans = min(ans,
              max(q.front().first,
                  (long long)max(v[i], v[(int)v.size() - 1] - v[i + k - 1])));
  }
  cout << ans << endl;
  return 0;
}
