#include <bits/stdc++.h>
using namespace std;
using edge = tuple<int, int>;
using pedge = vector<edge>;
const int N = 100010;
void BFS(const vector<pedge>& edges, vector<int>* dist, vector<edge>* prev) {
  int n = static_cast<int>(edges.size());
  queue<int> que;
  for (int i = 0; i < n; ++i) {
    if (dist->at(i) < (1000000001)) {
      que.push(i);
    }
  }
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (const auto& e : edges.at(u)) {
      int v, val;
      tie(v, val) = e;
      if (dist->at(v) > dist->at(u) + val) {
        dist->at(v) = dist->at(u) + val;
        if (prev) {
          prev->at(v) = make_tuple(u, val);
        }
        que.push(v);
      }
    }
  }
}
vector<edge> FindDiameter(const vector<pedge>& edges) {
  int n = static_cast<int>(edges.size());
  vector<int> dist(n);
  vector<edge> prev(n);
  int end = 0;
  fill(dist.begin(), dist.end(), (1000000001));
  dist.at(end) = 0;
  BFS(edges, &dist, &prev);
  for (int i = 0; i < n; ++i) {
    if (dist.at(end) < dist.at(i)) {
      end = i;
    }
  }
  int start = end;
  fill(dist.begin(), dist.end(), (1000000001));
  dist.at(start) = 0;
  BFS(edges, &dist, &prev);
  for (int i = 0; i < n; ++i) {
    if (dist.at(start) < dist.at(i)) {
      start = i;
    }
  }
  vector<edge> diameter = {make_tuple(start, 0)};
  for (int p = start; p != end; p = get<0>(prev.at(p))) {
    diameter.push_back(prev.at(p));
  }
  return diameter;
}
void solve(const vector<pedge>& edges, const int max_num_selected_point) {
  auto diameter = FindDiameter(edges);
  int ans = (1000000001);
  auto len = static_cast<int>(diameter.size());
  auto num_points = min(max_num_selected_point, len);
  partial_sum(diameter.begin(), diameter.end(), diameter.begin(),
              [](const edge& sum, const edge& e) -> edge {
                return make_tuple(get<0>(e), std::get<1>(sum) + get<1>(e));
              });
  auto diameter_len = get<1>(diameter.back());
  for (int i = num_points - 1; i < len; ++i) {
    ans = min(ans, max(get<1>(diameter.at(i - num_points + 1)),
                       diameter_len - get<1>(diameter.at(i))));
  }
  vector<int> dist(edges.size());
  fill(dist.begin(), dist.end(), (1000000001));
  for (const auto& p : diameter) {
    dist.at(get<0>(p)) = 0;
  }
  BFS(edges, &dist, nullptr);
  ans = max(ans, *max_element(dist.begin(), dist.end()));
  printf("%d\n", ans);
}
int main() {
  int n, k;
  vector<pedge> edges;
  scanf("%d%d", &n, &k);
  edges.resize(n);
  for (int i = 0, u, v, val; i < n - 1; ++i) {
    scanf("%d%d%d", &u, &v, &val);
    --u, --v;
    edges.at(u).push_back(make_pair(v, val));
    edges.at(v).push_back(make_pair(u, val));
  }
  solve(edges, k);
  return 0;
}
