#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const int MAX = 1e5 + 10;
class Edge {
 public:
  int s, e;
  int w;
  Edge(int s, int e, int w) : s(s), e(e), w(w) {}
};
class MaxQ {
 public:
  deque<int> q;
  void add(int e) {
    while (!q.empty() && q.back() < e) q.pop_back();
    q.push_back(e);
  }
  void remove(int e) {
    if (!q.empty() && q.front() == e) q.pop_front();
  }
  int getMax() { return q.empty() ? 0 : q.front(); }
};
vector<Edge> edges;
vector<vector<int>> adjList(MAX);
vector<int> dpt(MAX), mxd(MAX), secd(MAX);
vector<int> diameter;
void dfs(int nd, int p = -1, int depth = 0) {
  mxd[nd] = dpt[nd] = depth;
  for (int ed : adjList[nd]) {
    int nxt = nd ^ edges[ed].s ^ edges[ed].e;
    if (nxt != p) {
      dfs(nxt, nd, depth + edges[ed].w);
      mxd[nd] = max(mxd[nd], mxd[nxt]);
    }
  }
}
void get_diameter(int nd, int p = -1) {
  diameter.push_back(nd);
  vector<pair<int, int>> Vd = {{-1, -1}, {-1, -1}};
  for (int ed : adjList[nd]) {
    int nxt = nd ^ edges[ed].s ^ edges[ed].e;
    if (nxt != p) {
      pair<int, int> pp = {mxd[nxt], nxt};
      if (Vd[0].first == -1)
        Vd[0] = pp;
      else if (Vd[1].first == -1) {
        Vd[1] = pp;
        if (Vd[0].first < Vd[1].first) swap(Vd[0], Vd[1]);
      } else {
        if (Vd[0].first < pp.first) {
          Vd[1] = Vd[0];
          Vd[0] = pp;
        } else if (Vd[1].first < pp.first) {
          Vd[1] = pp;
        }
      }
    }
  }
  secd[nd] = 0;
  if (Vd[1].first != -1) secd[nd] = Vd[1].first - dpt[nd];
  if (Vd[0].first != -1) {
    get_diameter(Vd[0].second, nd);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--, v--;
    edges.emplace_back(u, v, d);
    adjList[u].push_back(i);
    adjList[v].push_back(i);
  }
  dfs(0);
  int root;
  for (int i = 0, mx = -1; i < n; i++) {
    if (dpt[i] > mx) {
      root = i;
      mx = dpt[i];
    }
  }
  fill(dpt.begin(), dpt.end(), 0);
  fill(mxd.begin(), mxd.end(), 0);
  dfs(root);
  get_diameter(root);
  int ans = 0;
  int dsz = diameter.size();
  k = min(k, dsz);
  vector<int> pref(dsz), suff(dsz);
  for (int i = 0; i < dsz; i++) {
    pref[i] = dpt[diameter[i]];
    suff[i] = mxd[diameter[i]] - dpt[diameter[i]];
  }
  MaxQ q;
  ans = suff[k - 1];
  for (int i = 0; i < k; i++) {
    q.add(secd[diameter[i]]);
    ans = max(ans, secd[diameter[i]]);
  }
  for (int i = 1; i <= dsz - k; i++) {
    q.remove(secd[diameter[i - 1]]);
    q.add(secd[diameter[i + k - 1]]);
    int it = q.getMax();
    ans = min(ans, max(it, max(pref[i], suff[i + k - 1])));
  }
  cout << ans << endl;
  return 0;
}
