#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g[100000];
vector<pair<int, int> > t[100000];
int n, k;
int dp[100000][2];
bool cmp(pair<int, int> f, pair<int, int> s) {
  if (dp[f.first][0] + f.second != dp[s.first][0] + s.second)
    return dp[f.first][0] + f.second > dp[s.first][0] + s.second;
  else
    return f.first < s.first;
}
void dfs(int cur, int par = -1) {
  for (int i = 0; i < g[cur].size(); i++) {
    if (g[cur][i].first == par) continue;
    dfs(g[cur][i].first, cur);
    t[cur].push_back(g[cur][i]);
  }
  sort(t[cur].begin(), t[cur].end(), cmp);
  for (int i = 0; i < min(k - 1, (int)t[cur].size()); i++) {
    dp[cur][0] += dp[t[cur][i].first][0] + t[cur][i].second;
  }
  int add = 0;
  if (t[cur].size() >= k) {
    add = dp[t[cur][k - 1].first][0] + t[cur][k - 1].second;
  }
  for (int i = 0; i < t[cur].size(); i++) {
    if (i < k) {
      dp[cur][1] = max(dp[cur][1], dp[cur][0] + dp[t[cur][i].first][1] -
                                       dp[t[cur][i].first][0] + add);
    } else {
      dp[cur][1] = max(dp[cur][1],
                       dp[cur][0] + dp[t[cur][i].first][1] + t[cur][i].second);
    }
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  dfs(0);
  cout << max(dp[0][0], dp[0][1]) << endl;
  return 0;
}
