#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 112345;
int n, k;
vector<pair<int, int>> g[MAXN];
int md[MAXN];
void dfs_md(int r, int p) {
  for (auto e : g[r]) {
    int x, d;
    tie(x, d) = e;
    if (x == p) continue;
    dfs_md(x, r);
    md[r] = max(md[r], d + md[x]);
  }
}
int ms[MAXN];
void dfs_ms(int r, int p, int lim) {
  ms[r] = 1;
  for (auto e : g[r]) {
    int x, d;
    tie(x, d) = e;
    if (x == p) continue;
    dfs_ms(x, r, lim);
  }
  int cnt = 0;
  for (auto e : g[r]) {
    int x, d;
    tie(x, d) = e;
    if (x == p) continue;
    if (md[x] + d <= lim) continue;
    cnt++;
    if (cnt > 1) {
      ms[r] = n + 1;
      break;
    }
    ms[r] += ms[x];
  }
}
bool dfs_can(int r, int p, int lim, int md_up) {
  if (md_up > lim) return false;
  int cnt = 0;
  int need = 1;
  int ff = md_up;
  int sf = 0;
  for (auto e : g[r]) {
    int x, d;
    tie(x, d) = e;
    if (x == p) continue;
    if (md[x] + d > sf) {
      if (md[x] + d > ff) {
        sf = ff;
        ff = md[x] + d;
      } else {
        sf = md[x] + d;
      }
    }
    if (md[x] + d <= lim) continue;
    cnt++;
    need += ms[x];
  }
  if (cnt <= 2 and need <= k) return true;
  for (auto e : g[r]) {
    int x, d;
    tie(x, d) = e;
    if (x == p) continue;
    if (dfs_can(x, r, lim, md[x] + d == ff ? sf + d : ff + d)) return true;
  }
  return false;
}
bool can(int lim) {
  dfs_ms(0, 0, lim);
  return dfs_can(0, 0, lim, 0);
}
int bs() {
  int lb = 0;
  int ub = n * 10000;
  while (lb < ub) {
    int mid = (lb + ub) / 2;
    if (can(mid)) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }
  return lb;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    g[u].push_back({v, d});
    g[v].push_back({u, d});
  }
  dfs_md(0, 0);
  cout << bs() << endl;
}
