#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct edge {
  int to, w;
  edge(int a = 0, int b = 0) { to = a, w = b; }
};
int deep[maxn], fa[maxn], store[maxn], tail, n, k;
vector<edge> graph[maxn];
void dfs(int node) {
  for (int i = 0; i < graph[node].size(); ++i) {
    edge e = graph[node][i];
    int to = e.to, w = e.w;
    if (fa[to] == 0) {
      if (deep[to] == -1) deep[to] = deep[node] + w;
      fa[to] = node;
      dfs(to);
    }
  }
}
int main() {
  scanf("%d%d", &n, &k);
  int from, to, w;
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d%d%d", &from, &to, &w);
    graph[from].push_back(edge(to, w));
    graph[to].push_back(edge(from, w));
  }
  memset(deep, -1, sizeof(deep));
  memset(fa, 0, sizeof(fa));
  fa[1] = -1;
  deep[1] = 0;
  dfs(1);
  int ed1 = 1, ed2;
  for (int i = 1; i <= n; ++i) {
    if (deep[i] > deep[ed1]) ed1 = i;
  }
  memset(deep, -1, sizeof(deep));
  memset(fa, 0, sizeof(fa));
  fa[ed1] = -1;
  deep[ed1] = 0;
  dfs(ed1);
  ed2 = ed1;
  for (int i = 1; i <= n; ++i) {
    if (deep[i] > deep[ed2]) ed2 = i;
  }
  int deepest = deep[ed2];
  for (int i = ed2; i != -1; i = fa[i]) {
    store[tail++] = i;
  }
  int l, r;
  if (tail <= k)
    l = 0, r = tail - 1;
  else {
    int L = 0;
    for (int i = 0; i < tail - k + 1; ++i) {
      if (max(deepest - deep[store[i]], deep[store[i + k - 1]]) <
          max(deepest - deep[store[L]], deep[store[L + k - 1]]))
        L = i;
    }
    l = L, r = l + k - 1;
  }
  memset(deep, -1, sizeof(deep));
  memset(fa, 0, sizeof(fa));
  fa[store[l]] = -1;
  for (int i = l; i <= r; ++i) {
    deep[store[i]] = 0;
  }
  dfs(store[l]);
  int ans = 0;
  for (int i = 1; i <= n; ++i) ans = max(ans, deep[i]);
  printf("%d\n", ans);
  return 0;
}
