#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, k;
struct edge {
  int to, cost;
};
vector<edge> e[MAXN];
vector<edge> child[MAXN];
edge par[MAXN];
long long cdis[MAXN];
void inittree(int now, int p) {
  for (auto next : e[now]) {
    if (next.to == p)
      continue;
    else {
      child[now].push_back(next);
      par[next.to] = {now, next.cost};
      inittree(next.to, now);
    }
  }
}
void initdis(int now) {
  long long maxdis = 0;
  for (auto next : child[now]) {
    initdis(next.to);
    maxdis = max(cdis[next.to] + next.cost, maxdis);
  }
  cdis[now] = maxdis;
}
long long len;
long long dfs(int now, bool parneed, long long pardis) {
  long long fstmax = 0, sndmax = 0;
  for (auto next : child[now]) {
    long long nextdis = cdis[next.to] + next.cost;
    if (nextdis >= fstmax) {
      sndmax = fstmax;
      fstmax = nextdis;
    } else if (nextdis >= sndmax) {
      sndmax = nextdis;
    }
  }
  if (parneed == false) {
    for (auto next : child[now]) {
      long long nextdis = cdis[next.to] + next.cost;
      if (nextdis == fstmax) {
        if (max(sndmax, pardis) + next.cost <= len) {
          return dfs(next.to, false, max(sndmax, pardis) + next.cost);
        }
      } else {
        if (max(fstmax, pardis) + next.cost <= len) {
          return dfs(next.to, false, max(fstmax, pardis) + next.cost);
        }
      }
    }
  }
  int cntneed = 0;
  for (auto next : child[now]) {
    long long nextdis = cdis[next.to] + next.cost;
    if (nextdis > len) cntneed++;
  }
  int ret = 1;
  int avail = 2;
  if (parneed || pardis > len) avail--;
  if (cntneed > avail) return 1e9;
  for (auto next : child[now]) {
    long long nextdis = cdis[next.to] + next.cost;
    if (nextdis > len) {
      ret += dfs(next.to, true, 0);
    }
  }
  return ret;
}
bool check() {
  int cnt = dfs(0, false, 0);
  if (cnt <= k) return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &k);
  long long sum = 0;
  for (int i = 0; i < n - 1; i++) {
    int u, v, d;
    scanf("%d%d%d", &u, &v, &d);
    u--;
    v--;
    e[u].push_back({v, d});
    e[v].push_back({u, d});
    sum += d;
  }
  par[0].to = -1;
  inittree(0, 0);
  initdis(0);
  long long l = -1, r = sum + 10;
  while (l + 1 != r) {
    long long mid = (l + r) / 2;
    len = mid;
    if (check())
      r = mid;
    else
      l = mid;
  }
  printf("%lld\n", r);
}
