#include <bits/stdc++.h>
using namespace std;
int N, K;
vector<pair<int, int> > graph[100010];
int dp1[100010], dp2[100010];
pair<int, int> cost[100010];
int sum[100010];
void dfs(int p, int x) {
  int i;
  vector<pair<int, int> > child;
  for ((i) = 0; (i) < (int)(graph[x].size()); (i)++) {
    int y = graph[x][i].first;
    if (y != p) {
      dfs(x, y);
      child.push_back(make_pair(y, graph[x][i].second));
    }
  }
  int C = child.size();
  for ((i) = 0; (i) < (int)(C); (i)++)
    cost[i] = make_pair(dp2[child[i].first] + child[i].second,
                        dp1[child[i].first] + child[i].second);
  sort(cost, cost + C);
  reverse(cost, cost + C);
  for ((i) = 0; (i) < (int)(min(C, K - 1)); (i)++)
    if (i < C) dp2[x] += cost[i].first;
  for ((i) = 0; (i) < (int)(C); (i)++) sum[i + 1] = sum[i] + cost[i].first;
  int tmp = 0;
  for ((i) = 0; (i) < (int)(C); (i)++) {
    int tmp2 = cost[i].second;
    if (i < K - 1) {
      tmp2 += sum[min(C, K)];
      tmp2 -= cost[i].first;
    } else {
      tmp2 += sum[min(C, K - 1)];
    }
    tmp = max(tmp, tmp2);
  }
  dp1[x] = tmp;
}
int main(void) {
  int i;
  cin >> N >> K;
  for ((i) = 0; (i) < (int)(N - 1); (i)++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  dfs(-1, 0);
  cout << dp1[0] << endl;
  return 0;
}
