#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> conn[N];
bitset<N> seen;
int dist[N], parent[N];
int parenta[N], parentb[N];
int dista[N], distb[N];
int n, t;
void dfs(int u, int d) {
  seen[u] = true;
  dist[u] = d;
  for (int i = 0; i < conn[u].size(); i += 2) {
    int v = conn[u][i], dd = conn[u][i + 1];
    if (!seen[v]) {
      dfs(v, d + dd);
    }
  }
}
void dfs2(int u, int prt, int d) {
  seen[u] = true;
  parent[u] = prt;
  dist[u] = d;
  for (int i = 0; i < conn[u].size(); i += 2) {
    int v = conn[u][i], dd = conn[u][i + 1];
    if (!seen[v]) {
      dfs2(v, u, d + dd);
    }
  }
}
int get() {
  int d = 0, x = 1;
  for (int i = 1; i <= n; ++i)
    if (dist[i] >= d) d = dist[i], x = i;
  return x;
}
int get_mid(int k) {
  int d = 2e9, x = 1;
  while (k != -1) {
    int dd = max(dista[k], distb[k]);
    if (dd <= d) {
      d = dd;
      x = k;
    }
    k = parenta[k];
  }
  return x;
}
int main() {
  scanf("%d %d", &n, &t);
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    conn[a].emplace_back(b);
    conn[a].emplace_back(c);
    conn[b].emplace_back(a);
    conn[b].emplace_back(c);
  }
  int a, b;
  dfs(1, 0);
  a = get();
  memset(dist, 0, sizeof dist);
  seen.reset();
  dfs(a, 0);
  b = get();
  seen.reset();
  dfs2(a, -1, 0);
  for (int i = 1; i <= n; ++i) {
    dista[i] = dist[i];
    parenta[i] = parent[i];
  }
  seen.reset();
  dfs2(b, -1, 0);
  for (int i = 1; i <= n; ++i) {
    distb[i] = dist[i];
    parentb[i] = parent[i];
  }
  int L, R;
  L = R = get_mid(b);
  seen.reset();
  seen[L] = true;
  vector<int> vec;
  vec.emplace_back(L);
  while (t-- > 1) {
    if (R == -1 or dista[L] >= distb[R]) {
      L = parenta[L];
      if (L != -1) {
        vec.emplace_back(L);
        seen[L] = true;
      }
    } else {
      R = parentb[R];
      if (R != -1) {
        vec.emplace_back(R);
        seen[R] = true;
      }
    }
  }
  memset(dist, 0, sizeof dist);
  for (int& a : vec) {
    dfs(a, 0);
  }
  int res = *max_element(dist, dist + n + 1);
  printf("%d\n", res);
}
