#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 1;
long long n, k, f[N], d[N], pd[N];
long long q[N], l, r, tot, deep, ans[N];
vector<long long> v[N], w[N];
void dfs(long long x) {
  long long n1;
  for (long long i = 0; i < v[x].size(); i++) {
    n1 = v[x][i];
    if (f[x] == n1) continue;
    f[n1] = x;
    d[n1] = d[x] + w[x][i];
    dfs(n1);
  }
}
long long dfs1(long long x) {
  long long n1, maxn = d[x];
  for (long long i = 0; i < v[x].size(); i++) {
    n1 = v[x][i];
    if (f[x] == n1 || pd[n1]) continue;
    maxn = max(maxn, dfs1(n1));
  }
  return maxn;
}
void zhx(long long x) {
  pd[x] = 1;
  ans[x] = 0;
  while (f[x] != x) {
    x = f[x];
    ans[x] = dfs1(x) - d[x];
    pd[x] = 1;
  }
}
signed main() {
  long long x, y, z;
  scanf("%lld%lld", &n, &k);
  for (long long i = 1; i < n; i++) {
    scanf("%lld%lld%lld", &x, &y, &z);
    v[x].push_back(y);
    v[y].push_back(x);
    w[x].push_back(z);
    w[y].push_back(z);
  }
  f[1] = 1;
  dfs(1);
  x = 1;
  for (long long i = 1; i <= n; i++)
    if (d[i] > d[x]) x = i;
  f[x] = x;
  d[x] = 0;
  dfs(x);
  y = x;
  for (long long i = 1; i <= n; i++)
    if (d[i] > d[y]) y = i;
  deep = d[y];
  zhx(y);
  z = y;
  l = r = 1;
  q[l] = y;
  for (long long i = 1; i < k; i++) {
    z = f[z];
    r++;
    q[r] = z;
    while (l < r && ans[q[r]] >= ans[q[r - 1]]) q[r - 1] = q[r], r--;
  }
  tot = max(ans[q[l]], d[z]);
  while (z != x) {
    z = f[z];
    r++;
    q[r] = z;
    if (q[l] == y) l++;
    y = f[y];
    while (l < r && ans[q[r]] >= ans[q[r - 1]]) q[r - 1] = q[r], r--;
    tot = min(tot, max(ans[q[l]], max(deep - d[y], d[z])));
  }
  printf("%lld", tot);
  return 0;
}
