#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 100;
struct E {
  int to;
  int val;
  int next;
} e[MAXN << 1];
struct node {
  int used;
  int num;
  int len;
};
int n, k, mid, flag, ac, tot, head[MAXN];
void add_edge(int u, int v, int val) {
  e[++tot].next = head[u];
  e[tot].to = v;
  e[tot].val = val;
  head[u] = tot;
}
int Dfs(int x, int f) {
  int len = 0;
  for (int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    if (to == f) continue;
    len = max(len, Dfs(to, x) + e[i].val);
  }
  return len;
}
node dfs(int x, int f, int L) {
  node now;
  now.used = now.len = now.num = 0;
  for (int i = head[x]; i; i = e[i].next) {
    int to = e[i].to;
    if (to == f) continue;
    node tmp = dfs(to, x, e[i].val);
    if (ac != -1) return now;
    tmp.len += e[i].val;
    now.len = max(now.len, tmp.len);
    if (tmp.used) {
      now.used++;
      now.num += tmp.num;
    } else if (tmp.len > mid) {
      now.used++;
      now.num++;
    }
  }
  if (k == 1 && L + now.len > mid) ac = (Dfs(f, x) + L <= mid);
  if (now.used) {
    now.num++;
    now.len = 0;
  }
  if (now.used > 2 || now.num > k) flag = 0;
  if (now.used == 2 && x != 1 && flag && ac == -1) ac = (Dfs(f, x) + L <= mid);
  if (now.num == k && x != 1 && flag && ac == -1) ac = (Dfs(f, x) + L <= mid);
  return now;
}
bool check() {
  flag = 1;
  ac = -1;
  dfs(1, -1, 0);
  if (ac != -1)
    return ac;
  else
    return flag;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, val;
    scanf("%d%d%d", &u, &v, &val);
    add_edge(u, v, val);
    add_edge(v, u, val);
  }
  int l = 0, r = 1e9, ans;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check()) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  cout << ans;
}
