#include <bits/stdc++.h>
using namespace std;
class Solution {
 private:
  int n;

 public:
  int maxminDistance(const vector<unordered_map<int, int>>& g, int k,
                     int sumD) {
    n = g.size();
    assert(n > 0);
    vector<int> degrees(n, 0);
    for (int i = 0; i < n; i++) degrees[i] = g[i].size();
    int l = 0, r = sumD;
    while (l < r) {
      int mid = l + (r - l) / 2;
      if (ok(g, k, mid, degrees))
        r = mid;
      else
        l = mid + 1;
    }
    assert(l == r);
    return l;
  }

 private:
  bool ok(const vector<unordered_map<int, int>>& g, int k, int maxD,
          vector<int> degrees) {
    queue<int> q;
    vector<int> d(n, 0);
    int notok = 0;
    for (int i = 0; i < n; i++)
      if (degrees[i] == 1)
        q.push(i);
      else if (degrees[i] > 2)
        notok++;
    vector<bool> deleted(n, false);
    int left = n;
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      for (const pair<int, int>& p : g[cur]) {
        int next = p.first;
        int nextDis = p.second;
        if (deleted[next]) continue;
        if (d[cur] + nextDis <= maxD) {
          deleted[cur] = true;
          degrees[next]--;
          d[next] = max(d[next], d[cur] + nextDis);
          left--;
          if (degrees[next] + 1 == 3) {
            notok--;
            if (notok == 0 && left <= k) return true;
          }
          if (degrees[next] == 1)
            q.push(next);
          else if (degrees[next] == 0)
            return true;
        }
      }
    }
    for (int i = 0; i < n; i++)
      if (!deleted[i] && degrees[i] > 2) return false;
    return left <= k;
  }
};
int main() {
  int n, k;
  cin >> n >> k;
  vector<unordered_map<int, int>> g(n);
  int u, v, d;
  int sumD = 0;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> d;
    u--, v--;
    g[u][v] = d, g[v][u] = d;
    sumD += d;
  }
  cout << Solution().maxminDistance(g, k, sumD) << endl;
  return 0;
}
