#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<int, int> > adj[100005];
int dp[100005][2];
struct info {
  int val, idx, cost;
  bool operator<(const info &i) const {
    if (val != i.val) return val > i.val;
    return idx < i.idx;
  }
};
void dfs(int now, int pre) {
  int child = 0;
  vector<info> tmp;
  for (int i = 0; i < adj[now].size(); i++) {
    int nxt = adj[now][i].first;
    if (nxt == pre) continue;
    ++child;
    dfs(nxt, now);
    tmp.push_back({(adj[now][i].second + dp[nxt][0]), nxt, adj[now][i].second});
  }
  if (child == 0) return;
  sort(tmp.begin(), tmp.end());
  int topk = 0;
  for (int i = 0; i < min(k - 1, child); i++) {
    topk += tmp[i].val;
  }
  dp[now][0] = topk;
  if (k - 1 < child) topk += tmp[k - 1].val;
  for (int i = 0; i < tmp.size(); i++) {
    int nxt = tmp[i].idx;
    if (i <= min(k - 1, child))
      dp[now][1] =
          max(dp[now][1], topk - tmp[i].val + tmp[i].cost + dp[nxt][1]);
    else
      dp[now][1] = max(dp[now][1], topk - tmp[min(k - 1, child)].val +
                                       tmp[i].cost + dp[nxt][1]);
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int a, b, c, i = 0; i < n - 1; i++) {
    scanf("%d %d %d", &a, &b, &c);
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  dfs(0, 0);
  int ans = dp[0][1];
  printf("%d\n", ans);
}
