#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> G[100005];
int dp[100005][2], k;
int ans;
bool mark[100005], vis[100005][2];
int dfs(int v, int p, int flag) {
  if (vis[v][flag]) return dp[v][flag];
  vis[v][flag] = true;
  vector<pair<int, int>> costs;
  for (auto& x : G[v]) {
    if (x.first == p) continue;
    costs.emplace_back(dfs(x.first, v, 0) + x.second, x.first);
  }
  sort(costs.begin(), costs.end(), greater<pair<int, int>>());
  if (!flag) {
    for (int i = 0; i < k - 1 && i < costs.size(); i++)
      dp[v][0] += costs[i].first;
    return dp[v][0];
  }
  int sum1 = 0;
  for (int i = 0; i < k - 1 && i < costs.size(); i++) {
    sum1 += costs[i].first;
    mark[costs[i].second] = true;
  }
  int sum2 = sum1;
  if (costs.size() >= k) sum2 += costs[k - 1].first;
  for (auto& x : G[v]) {
    if (x.first == p) continue;
    if (mark[x.first])
      dp[v][1] = max(dp[v][1], sum2 - dfs(x.first, v, 0) + dfs(x.first, v, 1));
    else
      dp[v][1] = max(dp[v][1], sum1 + dfs(x.first, v, 1) + x.second);
  }
  return dp[v][1];
}
int main() {
  int n, a, b, c;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &a, &b, &c);
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  printf("%d\n", dfs(0, 0, 1));
  return 0;
}
