#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int n, ans, k;
long long dp[N][2];
vector<vector<pair<int, int> > > adjList(N);
long long solve(int node, int parent, bool stop) {
  if (dp[node][stop] != -1) return dp[node][stop];
  vector<pair<long long, int> > chNonStop;
  for (int i = 0; i < adjList[node].size(); i++) {
    int ch = adjList[node][i].first;
    int cost = adjList[node][i].second;
    if (ch != parent) {
      long long temp = solve(ch, node, false);
      chNonStop.push_back(make_pair(temp + cost, i));
    }
  }
  sort(chNonStop.rbegin(), chNonStop.rend());
  long long ret = 0;
  if (!stop) {
    for (int i = 0; i < min((int)chNonStop.size(), k - 1); i++)
      ret += chNonStop[i].first;
  } else {
    for (int i = 0; i < min((int)chNonStop.size(), k - 1); i++)
      ret += chNonStop[i].first;
    long long extra = 0;
    if (chNonStop.size() >= k) extra = chNonStop[k - 1].first;
    long long tempAns = 0;
    for (int i = 0; i < chNonStop.size(); i++) {
      int ind = chNonStop[i].second;
      if (i < k - 1)
        tempAns = max(tempAns, ret + adjList[node][ind].second +
                                   solve(adjList[node][ind].first, node, true) -
                                   chNonStop[i].first + extra);
      else
        tempAns = max(
            tempAns,
            ret + adjList[node][chNonStop[i].second].second +
                solve(adjList[node][chNonStop[i].second].first, node, true));
    }
    ret = tempAns;
  }
  return dp[node][stop] = ret;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n - 1; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    adjList[x].push_back(make_pair(y, c));
    adjList[y].push_back(make_pair(x, c));
  }
  memset(dp, -1, sizeof dp);
  printf("%I64d", solve(0, 0, true));
  return 0;
}
