#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;
template <typename T>
inline void read(T &x) {
  x = 0;
  bool f = false;
  char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  x = f ? -x : x;
  return;
}
template <typename T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 ^ 48);
  return;
}
const int N = 1e5 + 5, INF = 1e9 + 7;
int n, k;
int a[N], nex[N << 1], to[N << 1], head[N], val[N << 1], idx;
inline void add(int u, int v, int w) {
  nex[++idx] = head[u];
  to[idx] = v;
  val[idx] = w;
  head[u] = idx;
  return;
}
int Maxn = -1, Id1, sta[N], top, ne[N], Val[N];
void dfs1(int x, int f, int dep) {
  if (dep > Maxn) Maxn = dep, Id1 = x;
  for (int i = head[x]; i; i = nex[i]) {
    int y = to[i];
    if (y == f) continue;
    dfs1(y, x, dep + val[i]);
  }
  return;
}
int dfs2(int x, int f, int dep) {
  int Max = dep;
  for (int i = head[x]; i; i = nex[i]) {
    int y = to[i];
    if (y == f) continue;
    int tmp = dfs2(y, x, dep + val[i]);
    if (tmp > Max) Max = tmp, ne[x] = y, Val[x] = val[i];
  }
  return Max;
}
int Ans, dis[N], d1[N], d2[N];
bool vis[N];
int GetRoad(int x, int f, int dep) {
  vis[x] = true;
  int Max = dep;
  for (int i = head[x]; i; i = nex[i]) {
    int y = to[i];
    if (y == f || vis[y]) continue;
    Max = max(Max, GetRoad(y, x, dep + val[i]));
  }
  return Max;
}
signed main() {
  read(n), read(k);
  Ans = INF;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    read(u), read(v), read(w);
    add(u, v, w), add(v, u, w);
  }
  dfs1(1, 0, 0);
  dfs2(Id1, 0, 0);
  int res = 0;
  for (int i = Id1; i; i = ne[i])
    sta[++top] = i, dis[top] = Val[i], vis[i] = true;
  for (int i = Id1; i; i = ne[i]) res = max(res, GetRoad(i, 0, 0));
  for (int i = 2; i <= top; i++) d1[i] = d1[i - 1] + dis[i - 1];
  for (int i = top - 1; i >= 1; i--) d2[i] = d2[i + 1] + dis[i];
  for (int i = 1; i + k - 1 <= top; i++)
    Ans = min(Ans, max(d1[i], d2[i + k - 1]));
  write(Ans == INF ? res : max(Ans, res));
  return 0;
}
