#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 7;
int n, k, ans, Root, dsub[maxN], max2[maxN], dpar[maxN];
int help[maxN];
bool mark[maxN];
vector<pair<int, int> > edges[maxN];
set<pair<int, int> > st;
void DFS(int u, int p) {
  for (int i = 0; i < edges[u].size(); i++) {
    int w = edges[u][i].first;
    int v = edges[u][i].second;
    if (v == p) continue;
    DFS(v, u);
    if (dsub[u] <= dsub[v] + w) {
      max2[u] = dsub[u];
      dsub[u] = dsub[v] + w;
    } else
      max2[u] = max(max2[u], dsub[v] + w);
  }
}
void DFS2(int u, int p) {
  for (int i = 0; i < edges[u].size(); i++) {
    int w = edges[u][i].first;
    int v = edges[u][i].second;
    if (v == p) continue;
    dpar[v] = dpar[u] + w;
    if (dsub[v] + w == dsub[u])
      dpar[v] = max(dpar[v], max2[u] + w);
    else
      dpar[v] = max(dpar[v], dsub[u] + w);
    DFS2(v, u);
  }
}
void Solve(int u, int par) {
  for (int i = 0; i < edges[u].size(); i++) {
    int w = edges[u][i].first;
    int v = edges[u][i].second;
    if (v == par) continue;
    dpar[v] = u;
    help[v] = w;
    Solve(v, u);
    dsub[u] = max(dsub[u], dsub[v] + w);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    edges[u].push_back(pair<int, int>(w, v));
    edges[v].push_back(pair<int, int>(w, u));
  }
  DFS(1, 1);
  DFS2(1, 1);
  int Max = 1000000000;
  for (int i = 1; i <= n; i++) {
    int X = max(dsub[i], dpar[i]);
    if (X < Max) {
      Max = X;
      Root = i;
    }
  }
  memset(dsub, 0, sizeof dsub);
  memset(dpar, 0, sizeof dpar);
  dpar[Root] = Root;
  Solve(Root, Root);
  Max = 0;
  int Sub1, Sub2;
  for (int i = 0; i < edges[Root].size(); i++) {
    int w = edges[Root][i].first;
    int v = edges[Root][i].second;
    if (Max < dsub[v] + w) {
      Max = dsub[v] + w;
      Sub1 = v;
    }
  }
  st.insert(pair<int, int>(-Max, Sub1));
  Max = 0;
  for (int i = 0; i < edges[Root].size(); i++) {
    int w = edges[Root][i].first;
    int v = edges[Root][i].second;
    if (v == Sub1) continue;
    if (Max < dsub[v] + w) {
      Max = dsub[v] + w;
      Sub2 = v;
    }
  }
  st.insert(pair<int, int>(-Max, Sub2));
  int cnt = 1;
  mark[Root] = true;
  while (1) {
    if (cnt == k) break;
    pair<int, int> Point = (*st.begin());
    st.erase(Point);
    int dis = Point.first;
    int u = Point.second;
    mark[u] = true;
    cnt++;
    if (cnt == k) break;
    int Max = 0, Sub = -1;
    for (int i = 0; i < edges[u].size(); i++) {
      int w = edges[u][i].first;
      int v = edges[u][i].second;
      if (v == dpar[u]) continue;
      if (Max < dsub[v] + w) {
        Max = dsub[v] + w;
        Sub = v;
      }
    }
    if (Sub != -1) st.insert(pair<int, int>(-Max, Sub));
    if (st.empty()) break;
  }
  for (int i = 1; i <= n; i++)
    if (mark[i] == false) ans = max(ans, dsub[i] + help[i]);
  cout << ans;
  return 0;
}
