#include <bits/stdc++.h>
using namespace std;
int n, k, root, root2 = -1;
vector<pair<long long, long long> > g[100500];
long long dist[100500];
long long asd[100500], dd[100500];
int from[1 << 20], fromw[1 << 20];
pair<long long, long long> best;
void ddfs(long long u, long long p, long long d) {
  best = max(best, make_pair(d, u));
  for (pair<long long, long long> e : g[u]) {
    long long v = e.first;
    long long w = e.second;
    if (v != p) {
      ddfs(v, u, d + w);
      from[v] = u;
      fromw[v] = w;
    }
  }
}
int center() {
  ddfs(0, -1, 0);
  long long st = best.second;
  best = make_pair(0, 0);
  ddfs(st, -1, 0);
  long long ed = best.second;
  long long len = best.first;
  long long cur = 0;
  pair<long long, long long> best = make_pair(len, ed);
  while (ed != st) {
    best = min(best, make_pair(abs(cur + cur - len), ed));
    cur += fromw[ed];
    ed = from[ed];
  }
  best = min(best, make_pair(abs(cur + cur - len), ed));
  return best.second;
}
void dfs(int u, int p, long long b) {
  dd[u] = 0;
  if (((int)(g[u]).size()) == 1) return;
  for (pair<long long, long long> v : g[u]) {
    if (v.first == p) continue;
    dfs(v.first, u, b);
    if (dd[v.first] + v.second > b) {
      asd[v.first] = 1;
    } else
      dd[u] = max(dd[u], dd[v.first] + v.second);
  }
}
int fa[100500];
void dfs2(int u, int p) {
  fa[u] = p;
  for (pair<long long, long long> v : g[u]) {
    if (v.first == p) continue;
    dfs2(v.first, u);
    asd[u] |= asd[v.first];
  }
}
bool can(long long m) {
  int r = 0;
  memset(asd, 0, sizeof asd);
  memset(fa, -1, sizeof fa);
  int le = 0;
  dfs(root, -1, m);
  dfs2(root, -1);
  asd[root] = 1;
  for (int i = 0; i < n; i++) {
    if (asd[i] == 0) continue;
    int p = fa[i];
    if (p == -1 || asd[p]) {
      bool b = 0;
      for (pair<long long, long long> v : g[i]) {
        if (v.first == p) continue;
        b |= asd[v.first];
      }
      if (!b) le++;
    }
  }
  for (int i = 0; i < n; i++) r += asd[i] != 0;
  return r <= k && le <= 2;
}
bool vis[100500];
int father[100500];
int bfs(int u) {
  queue<int> q;
  dist[u] = 0;
  memset(vis, 0, sizeof vis);
  q.push(u);
  father[u] = -1;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    for (pair<long long, long long> v : g[u]) {
      if (vis[v.first]) continue;
      father[v.first] = u;
      dist[v.first] = dist[u] + v.second;
      q.push(v.first);
      vis[v.first] = 1;
    }
  }
  pair<long long, int> mx = {0, 0};
  for (int i = 0; i < n; i++) mx = max(mx, {dist[i], i});
  return mx.second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  root = center();
  long long l = -1, r = 1e12;
  while (r - l > 1) {
    long long m = (l + r) / 2;
    if (can(m))
      r = m;
    else
      l = m;
  }
  cout << r << endl;
}
