#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to;
  long long cost;
};
vector<vector<edge> > g;
vector<long long> dist;
vector<pair<long long, long long> > p;
void dfs(int v, long long x) {
  dist[v] = x;
  for (edge& e : g[v]) {
    int to = e.to;
    if (dist[to] != -1) {
      continue;
    }
    p[to] = {v, e.cost};
    dfs(to, x + e.cost);
  }
}
const long long inf = 1e18;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  if (n == 1) {
    cout << "0\n";
    return 0;
  }
  g.resize(n);
  p.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    long long c;
    cin >> u >> v >> c;
    --u, --v;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
  }
  dist.assign(n, -1);
  dfs(0, 0);
  int u = 0;
  for (int i = 1; i < n; ++i) {
    if (dist[i] > dist[u]) {
      u = i;
    }
  }
  dist.assign(n, -1);
  dfs(u, 0);
  int v = 0;
  for (int i = 1; i < n; ++i) {
    if (dist[i] > dist[v]) {
      v = i;
    }
  }
  vector<int> vct;
  vector<int> path;
  path.push_back(v);
  while (v != u) {
    vct.push_back(p[v].second);
    v = p[v].first;
    path.push_back(v);
  }
  dist.assign(n, -1);
  for (int x : path) {
    dist[x] = 0;
  }
  for (int x : path) {
    dfs(x, 0);
  }
  long long z = 0;
  for (int i = 0; i < n; ++i) {
    z = max(z, dist[i]);
  }
  vector<long long> pref((int)(vct).size());
  pref[0] = vct[0];
  for (int i = 1; i < (int)(vct).size(); ++i) {
    pref[i] = pref[i - 1] + vct[i];
  }
  vector<long long> suff((int)(vct).size());
  suff.back() = vct.back();
  for (int i = (int)(vct).size() - 2; i >= 0; --i) {
    suff[i] = suff[i + 1] + vct[i];
  }
  long long ans = inf;
  for (int i = 0; i < (int)(vct).size(); ++i) {
    long long val = 0;
    if (i) {
      val = max(val, pref[i - 1]);
    }
    if (i + k - 1 < (int)(vct).size()) {
      val = max(val, suff[i + k - 1]);
    }
    ans = min(ans, val);
  }
  cout << max(ans, z) << "\n";
}
