#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
using pii = pair<int, int>;
int dp[maxn][2];
vector<pii> v[maxn];
int n, k;
void dfs(int now, int pre) {
  if ((int)v[now].size() == 1 && (now != 0)) return;
  int n = (int)v[now].size();
  if (now != 0) n--;
  vector<int> sum(n, 0);
  vector<pii> tmp;
  for (auto x : v[now]) {
    if (x.first == pre) continue;
    dfs(x.first, now);
    tmp.push_back(
        make_pair(dp[x.first][0] + x.second, dp[x.first][1] + x.second));
  }
  sort(tmp.begin(), tmp.end(),
       [](pii &a, pii &b) -> bool { return a.first > b.first; });
  int res = 0;
  sum[0] = tmp[0].first;
  for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + tmp[i].first;
  int x = (now == 0) ? k : k - 1;
  for (int i = 0; i < n && i < x; ++i) {
    res += tmp[i].first;
  }
  int res2 = 0;
  for (int i = 0; i < n; ++i) {
    int l = i - 1;
    if (l >= k - 1) l = k - 2;
    int tp = 0;
    if (l >= 0) tp += sum[l];
    if (l < k - 2) {
      int num = k - l - 2;
      int r = min(n - 1, i + num);
      tp += sum[r] - sum[i];
    }
    tp += tmp[i].second;
    res2 = max(res2, tp);
  }
  dp[now][0] = res;
  dp[now][1] = res2;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1, a, b, c; i < n; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  dfs(0, -1);
  printf("%d\n", max(dp[0][1], dp[0][0]));
}
