#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<vector<pair<int, int> > > edges;
int parent[100000];
int dist[100000];
void dfs(int x, int d) {
  dist[x] = d;
  for (int i = 0; i < edges[x].size(); i++) {
    int y = edges[x][i].first;
    int dst = edges[x][i].second;
    if (y == parent[x]) continue;
    parent[y] = x;
    dfs(y, d + dst);
  }
}
int height[100000];
int dfs2(int x, int d) {
  dist[x] = d;
  int h = 0;
  for (int i = 0; i < edges[x].size(); i++) {
    int y = edges[x][i].first;
    int dst = edges[x][i].second;
    if (y == parent[x]) continue;
    parent[y] = x;
    h = max(h, dst + dfs2(y, d + dst));
  }
  height[x] = h;
  return h;
}
int fmid() {
  parent[0] = -1;
  dfs(0, 0);
  int mx = 0, mxi = 0;
  for (int i = 0; i < N; i++) {
    if (dist[i] > mx) {
      mx = dist[i];
      mxi = i;
    }
  }
  int p1 = mxi;
  parent[p1] = -1;
  dfs(p1, 0);
  mx = mxi = 0;
  for (int i = 0; i < N; i++) {
    if (dist[i] > mx) {
      mx = dist[i];
      mxi = i;
    }
  }
  int p2 = mxi;
  int diameter = dist[p2];
  int mn = diameter, mni = p2;
  int p3 = p2;
  while (p3 != -1) {
    if (abs(2 * dist[p3] - diameter) < mn) {
      mn = abs(2 * dist[p3] - diameter);
      mni = p3;
    }
    p3 = parent[p3];
  }
  return mni;
}
set<pair<int, int> > S;
int done[100000] = {0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> K;
  if (N == 1) {
    cout << 0 << endl;
    exit(0);
  }
  edges.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back(make_pair(b, c));
    edges[b].push_back(make_pair(a, c));
  }
  int mid = fmid();
  parent[mid] = -1;
  dfs2(mid, 0);
  for (int i = 0; i < edges[mid].size(); i++) {
    int y = edges[mid][i].first;
    int dst = edges[mid][i].second;
    if (y == parent[mid]) continue;
    S.insert(make_pair(-(height[y] + dst), y));
  }
  K--;
  int ans = -1;
  done[mid] = -1;
  while (K--) {
    pair<int, int> t = *S.begin();
    S.erase(S.begin());
    int x = t.second;
    if (done[parent[x]] > 0) {
      ans = -t.first;
      break;
    }
    done[parent[x]]++;
    for (int i = 0; i < edges[x].size(); i++) {
      int y = edges[x][i].first;
      int dst = edges[x][i].second;
      if (y == parent[x]) continue;
      S.insert(make_pair(-(height[y] + dst), y));
    }
  }
  if (ans == -1) ans = -(*S.begin()).first;
  cout << ans << endl;
  return 0;
}
