#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
inline char gc() {
  static char buf[1 << 16], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin);
    if (p2 == p1) return EOF;
  }
  return *p1++;
}
template <class t>
inline t read(t &x) {
  char c = gc();
  bool f = 0;
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = gc();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
  if (f) x = -x;
  return x;
}
template <class t>
inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const int N = 1e5 + 5;
int n, k, ans, en, h[N], p, q, sum[N], dis[N], id[N], ma, dep;
bool exi[N];
struct edge {
  int n, v, w;
} e[N << 1];
void add(int x, int y, int z) {
  e[++en] = (edge){h[x], y, z};
  h[x] = en;
}
void dfs1(int x, int fa, int dis) {
  sum[x] = dis;
  for (int i = h[x]; i; i = e[i].n) {
    int y = e[i].v;
    if (y == fa) continue;
    dfs1(y, x, dis + e[i].w);
  }
  if (dis > ma) ma = dis, q = x;
}
void dfs2(int x, int fa, int d) {
  for (int i = h[x]; i; i = e[i].n) {
    int y = e[i].v;
    if (y == fa) continue;
    dfs2(y, x, d + 1);
    exi[x] |= exi[y];
    if (!exi[y]) dis[x] = max(dis[x], dis[y] + e[i].w);
  }
  if (exi[x]) id[d] = x, dep = max(dep, d);
}
bool check(int lim) {
  int l = 1, r = 0, ma = 0;
  for (int i = dep - 1; i; i--)
    if (sum[id[dep]] - sum[id[i]] > lim) {
      r = i + 1;
      break;
    }
  l = max(1, r - k + 1);
  for (int i = r; i >= l; i--) ma = max(ma, dis[id[i]]);
  return max(ma, sum[id[l]]) <= lim;
}
signed main() {
  read(n);
  read(k);
  for (int i = 1, x, y, z; i < n; i++)
    read(x), read(y), read(z), add(x, y, z), add(y, x, z);
  dfs1(1, 0, 0);
  ma = 0;
  dfs1(p = q, 0, 0);
  exi[q] = 1;
  dfs2(p, 0, 1);
  int l = 0, r = 1e9;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  write(ans);
}
