#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
map<int, int> a[N];
int n, k;
void dfs(int x, int par, int curr, int &mx, int &end) {
  if (curr > mx) {
    mx = curr;
    end = x;
  }
  for (auto [ch, _] : a[x]) {
    if (ch != par) {
      dfs(ch, x, curr + a[x][ch], mx, end);
    }
  }
}
void get_diameter(int x, int par, int d2, vector<int> &accum,
                  vector<int> &diam) {
  accum.push_back(x);
  if (x == d2) {
    diam = accum;
    accum.pop_back();
    return;
  }
  for (auto [ch, _] : a[x])
    if (ch != par) {
      get_diameter(ch, x, d2, accum, diam);
    }
  accum.pop_back();
}
int get_max_path_len(int x, int n1, int n2) {
  int ans = 0;
  for (auto [ch, _] : a[x]) {
    if (ch != n1 && ch != n2) {
      ans = max(ans, a[x][ch] + get_max_path_len(ch, x, n2));
    }
  }
  return ans;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    a[u][v] = c;
    a[v][u] = c;
  }
  int d1 = 1, d2 = 1, diameter = 0;
  dfs(1, 0, 0, diameter, d1);
  diameter = 0;
  dfs(d1, 0, 0, diameter, d2);
  vector<int> accum, diam;
  get_diameter(d1, 0, d2, accum, diam);
  assert(d1 != 0 && d2 != 0);
  int nd = diam.size();
  k = min(nd, k);
  vector<int> left(nd), right(nd), mid(nd);
  left[0] = 0;
  right[nd - 1] = 0;
  for (int i = 1; i < nd; i++) left[i] = left[i - 1] + a[diam[i - 1]][diam[i]];
  for (int i = nd - 2; i >= 0; i--)
    right[i] = right[i + 1] + a[diam[i + 1]][diam[i]];
  for (int i = 1; i < (nd - 1); i++) {
    mid[i] = get_max_path_len(diam[i], diam[i - 1], diam[i + 1]);
  }
  int ans = 1e9 + 5;
  if (k == 1) {
    for (int i = 0; i < nd; i++) {
      ans = min(ans, max(left[i], right[i]));
    }
    cout << ans;
    return 0;
  }
  multiset<int> ms;
  ms.insert(left[0]);
  for (int i = 1; i < k - 1; i++) ms.insert(mid[i]);
  ms.insert(right[k - 1]);
  ans = min(ans, *(--ms.end()));
  for (int i = 1; i + k <= nd; i++) {
    ms.erase(left[i - 1]);
    ms.erase(right[i + k - 2]);
    if (k >= 3) ms.erase(mid[i]);
    ms.insert(left[i]);
    ms.insert(right[i + k - 1]);
    if (k >= 3) ms.insert(mid[i + k - 2]);
    if (ms.size()) {
      ans = min(ans, *(--ms.end()));
    }
  }
  cout << ans;
  return 0;
}
