#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005, INF = 0x3f3f3f3f;
template <size_t N, size_t M, typename T = bool>
struct Adjacency_list {
  struct Edge {
    int to;
    T value;
    Edge* next;
  } * edges[N], pool[M], *alloc;
  Edge* operator[](int i) const { return edges[i]; }
  void clear() {
    alloc = pool;
    memset(edges, 0, sizeof edges);
  }
  Edge* new_Edge(int from, int to, T value) {
    Edge* ret = alloc++;
    ret->to = to;
    ret->value = value;
    ret->next = edges[from];
    return ret;
  }
  void add_edge(int from, int to, T value = true, int d = 1) {
    edges[from] = new_Edge(from, to, value);
    if (d == 2) edges[to] = new_Edge(to, from, value);
  }
};
int n, k;
Adjacency_list<MAXN, MAXN * 2, int> edges;
int dist[MAXN], parent[MAXN];
bool vis[MAXN];
void dfs(int x, int y, int& res) {
  parent[x] = y;
  if (dist[x] > dist[res]) res = x;
  for (auto i = edges[x]; i; i = i->next)
    if (i->to != y) {
      dist[i->to] = dist[x] + i->value;
      dfs(i->to, x, res);
    }
}
void dfs(int x, int y) {
  for (auto i = edges[x]; i; i = i->next)
    if (i->to != y) {
      dist[i->to] = (vis[i->to] ? 0 : dist[x] + i->value);
      dfs(i->to, x);
    }
}
int main() {
  scanf("%d%d", &n, &k);
  edges.clear();
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    edges.add_edge(u, v, w, 2);
  }
  int a = 1, b;
  dfs(1, 0, a);
  dist[b = a] = 0;
  dfs(a, 0, b);
  vector<int> v;
  for (int i = b; i; i = parent[i]) {
    vis[i] = true;
    v.push_back(i);
  }
  int ans = ((int)v.size() < k ? 0 : INF);
  for (int i = 0; i + k - 1 < (int)v.size(); i++)
    ans = min(ans, max(dist[b] - dist[v[i]], dist[v[i + k - 1]]));
  dfs(a, 0);
  for (int i = 1; i <= n; i++) ans = max(ans, dist[i]);
  printf("%d\n", ans);
  return 0;
}
