#include <bits/stdc++.h>
using namespace std;
const int maxi = 100005;
int all[maxi * 2], a1[maxi * 2], a2[maxi * 2], a, b, b1[maxi], b2[maxi], n, m,
    b3[maxi], b4[maxi], b5[maxi], k, ans;
bool bandera;
inline void ag(int x, int y, int z) {
  all[++a] = y, a1[a] = b2[x], b2[x] = a, a2[a] = z;
}
void fdfs(int x, int fa) {
  for (int i = b2[x]; i; i = a1[i])
    if (all[i] != fa) fdfs(all[i], x), b4[x] = max(b4[x], b4[all[i]] + a2[i]);
}
void sdfs(int x, int fa, int L) {
  int aa = b1[x];
  vector<int> ab1(aa + 1);
  vector<int> ab2(aa + 1);
  b5[x] = ab1[0] = ab2[0] = L;
  for (int i = b2[x], j = 0; i; i = a1[i])
    if (all[i] != fa) {
      j++;
      ab1[j] = ab2[j] = b4[all[i]] + a2[i];
    }
  for (int i = 1; i < aa; i++) ab1[i] = max(ab1[i], ab1[i - 1]);
  ab2[aa] = 0;
  for (int i = aa - 1; i; i--) ab2[i] = max(ab2[i], ab2[i + 1]);
  for (int i = b2[x], j = 0; i; i = a1[i])
    if (all[i] != fa) {
      j++;
      sdfs(all[i], x, max(ab1[j - 1], ab2[j + 1]) + a2[i]);
    }
}
void dfs(int x, int fa) {
  int M = 0, C = 0;
  for (int i = b2[x]; i; i = a1[i])
    if (all[i] != fa) {
      dfs(all[i], x);
      if (b4[all[i]] + a2[i] > b) {
        if (!M)
          M = b3[all[i]];
        else if (!C)
          C = b3[all[i]];
        else {
          M = C = k;
          break;
        }
      }
    }
  b3[x] = C ? k : M + 1;
  bandera |= (b5[x] <= b && M + C < k);
}
inline bool res() {
  memset(b3, 0, sizeof(b3)), bandera = 0;
  dfs(1, 0);
  return bandera;
}
int main() {
  cin >> n >> k;
  for (int i = 1, aaa, bbb, ccc; i < n; i++) {
    cin >> aaa >> bbb >> ccc;
    ag(aaa, bbb, ccc), ag(bbb, aaa, ccc);
    b1[aaa]++, b1[bbb]++;
  }
  fdfs(1, 0), b1[1]++, sdfs(1, 0, 0);
  int L = 0, R = 1e9;
  while (L <= R) {
    b = L + R >> 1;
    if (res())
      ans = b, R = b - 1;
    else
      L = b + 1;
  }
  cout << ans;
  return 0;
}
