#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf("%c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const long long &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
int n, k;
vector<pair<int, int> > g[100005];
int path[100005];
int val[100005], cnt;
int dp[100005];
pair<int, int> to[100005];
void dfs(int u, int p) {
  dp[u] = 0;
  to[u] = {0, 0};
  for (auto v : g[u]) {
    if (v.first == p) continue;
    dfs(v.first, u);
    if (dp[u] < dp[v.first] + v.second) {
      to[u] = v;
      dp[u] = dp[v.first] + v.second;
    }
  }
}
void dfs2(int u, int p1, int p2) {
  dp[u] = 0;
  to[u] = {0, 0};
  for (auto v : g[u]) {
    if (v.first == p1 || v.first == p2) continue;
    dfs2(v.first, u, 0);
    if (dp[u] < dp[v.first] + v.second) {
      to[u] = v;
      dp[u] = dp[v.first] + v.second;
    }
  }
}
int q[100005], ql, qr;
int id[100005];
int main() {
  R(n, k);
  for (int i = 1, u, v, w; i < n; ++i) {
    R(u, v, w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs(1, 0);
  int f1 = 1;
  while (to[f1].first) f1 = to[f1].first;
  dfs(f1, 0);
  int f2 = f1;
  path[++cnt] = f1;
  val[cnt] = 0;
  while (to[f2].first) {
    val[cnt + 1] = val[cnt] + to[f2].second;
    f2 = to[f2].first;
    path[++cnt] = f2;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 2; i < cnt; ++i) {
    dfs2(path[i], path[i - 1], path[i + 1]);
  }
  int mx = 2e9;
  for (int i = 1; i <= cnt; ++i) {
    int vv = max(val[max(1, i - k + 1)], val[cnt] - val[i]);
    while (qr > ql && q[qr - 1] <= dp[path[i]]) qr--;
    while (qr > ql && id[ql] < i - k + 1) ql++;
    q[qr] = dp[path[i]];
    id[qr++] = i;
    vv = max(vv, q[ql]);
    mx = min(mx, vv);
  }
  W(mx);
  return 0;
}
