#include <bits/stdc++.h>
using namespace std;
multiset<pair<int, int> > dist[100000 + 1];
multiset<pair<int, int> >::iterator it, it2;
vector<pair<int, int> > L[100000 + 1];
queue<int> shops;
int parent[100000 + 1];
int maxim[100000 + 1];
int up[100000 + 1];
int N, K;
void DFS_1(int node, int father) {
  dist[node].clear();
  dist[node].insert({0, node});
  for (auto child : L[node])
    if (child.first != father) {
      DFS_1(child.first, node);
      parent[child.first] = node;
      it = dist[child.first].end();
      it--;
      dist[node].insert({it->first + child.second, child.first});
    }
}
void DFS_2(int node, int father) {
  it2 = it = dist[node].end();
  it--;
  it2--;
  it2--;
  for (auto child : L[node])
    if (child.first != father) {
      up[child.first] = up[node] + child.second;
      if (child.first != it->second) {
        up[child.first] = max(up[child.first], it->first + child.second);
      } else {
        up[child.first] = max(up[child.first], it2->first + child.second);
      }
    }
  maxim[node] = max(up[node], it->first);
  for (auto child : L[node])
    if (child.first != father) DFS_2(child.first, node);
}
int main() {
  int i, first, second, d, root, best, head1, head2, old, res = 0;
  cin >> N >> K;
  for (i = 1; i < N; i++) {
    cin >> first >> second >> d;
    L[first].push_back({second, d});
    L[second].push_back({first, d});
  }
  DFS_1(1, 0);
  DFS_2(1, 0);
  best = 1 << 30;
  for (i = 1; i <= N; i++)
    if (maxim[i] < best) best = maxim[i], root = i;
  DFS_1(root, 0);
  if (K == 1) {
    cout << best;
    return 0;
  }
  it = dist[root].end();
  it--;
  K = K - 2;
  head1 = root;
  head2 = it->second;
  shops.push(head1);
  shops.push(head2);
  dist[root].erase(it);
  while (K > 0) {
    it = dist[head1].end();
    it--;
    it2 = dist[head2].end();
    it2--;
    if (it->first > it2->first) {
      old = head1;
      head1 = it->second;
      shops.push(head1);
      dist[old].erase(it);
    } else {
      if (it2->first == 0) break;
      old = head2;
      head2 = it2->second;
      shops.push(head2);
      dist[old].erase(it2);
    }
    --K;
  }
  while (!shops.empty()) {
    int node = shops.front();
    shops.pop();
    it = dist[node].end();
    it--;
    res = max(res, it->first);
  }
  cout << res;
  return 0;
}
