#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 10;
vector<pair<int, int>> adj[MAX_N];
int mx_up[MAX_N], mx_down[MAX_N], mx_dist;
void dfs_down(int u, int par = -1) {
  for (auto x : adj[u]) {
    int v = x.first, w = x.second;
    if (v != par) {
      dfs_down(v, u);
      mx_down[u] = max(mx_down[u], mx_down[v] + w);
    }
  }
}
void dfs_up(int u, int par = -1) {
  int mx1 = 0, mx2 = 0;
  for (auto x : adj[u]) {
    int v = x.first, w = x.second;
    if (v != par)
      mx1 < mx_down[v] + w ? mx2 = mx1, mx1 = mx_down[v] + w
                           : mx2 = max(mx2, mx_down[v] + w);
  }
  for (auto x : adj[u]) {
    int v = x.first, w = x.second;
    if (v != par)
      mx_up[v] = max(mx_up[u], (mx_down[v] + w != mx1 ? mx1 : mx2)) + w,
      dfs_up(v, u);
  }
}
int dfs(int u, int par = -1, bool up = false) {
  vector<pair<int, int>> vec;
  for (auto x : adj[u]) {
    int v = x.first, w = x.second;
    if (v != par && mx_down[v] + w > mx_dist) vec.push_back(x);
  }
  if (vec.size() > 2) return MAX_N;
  if (vec.size() == 2) {
    if (up) return MAX_N;
    int ans = 1;
    for (auto x : vec) ans += dfs(x.first, u, true);
    return ans;
  }
  if (vec.size() == 1) {
    int mx = mx_up[u];
    for (auto x : adj[u]) {
      int v = x.first, w = x.second;
      if (v != par && x != vec[0]) mx = max(mx, mx_down[v] + w);
    }
    int v = vec[0].first, w = vec[0].second;
    if (!up && mx + w <= mx_dist) return dfs(v, u, false);
    return dfs(v, u, true) + 1;
  }
  return 1;
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
  }
  dfs_down(0), dfs_up(0);
  int L = -1, R = 1e9 + 10;
  while (L + 1 < R) {
    mx_dist = (L + R) / 2;
    dfs(0) <= k ? R = mx_dist : L = mx_dist;
  }
  cout << R << endl;
  return 0;
}
