#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100007;
const int MAXX = 1000000000;
vector<pair<int, int> > edg[MAXN];
int n, k;
int niche[MAXN];
int par[MAXN], lvl[MAXN];
void dfs0(int u, int p) {
  niche[u] = 0;
  par[u] = p;
  lvl[u] = lvl[p] + 1;
  for (pair<int, int> pr : edg[u]) {
    if (p == pr.first) continue;
    dfs0(pr.first, u);
    niche[u] = max(niche[u], pr.second + niche[pr.first]);
  }
}
vector<int> inside;
int dnow;
bool dfs1(int u, int p, int pc) {
  bool flg = false;
  for (pair<int, int> pr : edg[u]) {
    if (p == pr.first) continue;
    if (dfs1(pr.first, u, pr.second)) flg = true;
  }
  if (!flg && pc + niche[u] > dnow) {
    inside.push_back(u);
    flg = true;
  }
  return flg;
}
queue<int> q;
int vis[MAXN];
int dist[MAXN];
void onePath(int u) {
  while (u > 0 && q.size() < k) {
    vis[u] = 1;
    dist[u] = 0;
    q.push(u);
    u = par[u];
  }
}
void twoPath(int u, int v) {
  if (lvl[u] < lvl[v]) swap(u, v);
  while (lvl[u] > lvl[v]) {
    vis[u] = 1;
    dist[u] = 0;
    q.push(u);
    u = par[u];
  }
  while (u != v) {
    vis[u] = vis[v] = 1;
    dist[u] = dist[v] = 0;
    q.push(u);
    q.push(v);
    u = par[u];
    v = par[v];
  }
  q.push(u);
  vis[u] = 1;
  dist[u] = 0;
}
bool bfs() {
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (pair<int, int> pr : edg[u]) {
      if (vis[pr.first]) continue;
      dist[pr.first] = dist[u] + pr.second;
      if (dist[pr.first] > dnow) return false;
      q.push(pr.first);
      vis[pr.first] = 1;
    }
  }
  return true;
}
bool canDo() {
  inside.clear();
  dfs1(1, 0, 0);
  if (inside.size() == 0) return true;
  if (inside.size() > 2) return false;
  q = queue<int>();
  memset(vis, 0, sizeof vis);
  if (inside.size() == 1) {
    onePath(inside[0]);
  } else {
    twoPath(inside[0], inside[1]);
    if (q.size() > k) return false;
  }
  return bfs();
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    edg[u].emplace_back(v, c);
    edg[v].emplace_back(u, c);
  }
  dfs0(1, 0);
  int l = 0, r = MAXX;
  while (l <= r) {
    dnow = (l + r) / 2;
    if (canDo())
      r = dnow - 1;
    else
      l = dnow + 1;
  }
  cout << l << endl;
  return 0;
}
