#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
int n, k;
vector<pair<int, int> > g[M];
int ans;
int dp[M][2];
void dfs(int u, int Pa) {
  vector<pair<int, int> > ch;
  for (auto p : g[u]) {
    int v = p.first, w = p.second;
    if (v == Pa) continue;
    dfs(v, u);
    ch.push_back(make_pair(dp[v][0] + w, dp[v][1] + w));
  }
  bool flag = ch.size() > 0;
  if (flag) {
    sort(ch.begin(), ch.end(), [&](pair<int, int> a, pair<int, int> b) {
      return a.second > b.second;
    });
  }
  for (int i = 0; i < k - 1 && i < ch.size(); i++) {
    dp[u][1] += ch[i].second;
  }
  if (ch.size() <= k) {
    for (auto p : ch) {
      dp[u][0] += p.second;
    }
    if (flag) {
      sort(ch.begin(), ch.end(), [&](pair<int, int> a, pair<int, int> b) {
        return a.first - a.second > b.first - b.second;
      });
      dp[u][0] += ch[0].first - ch[0].second;
    }
  } else {
    int max_val = -1e9;
    for (int i = k; i < ch.size(); i++) {
      max_val = max(max_val, ch[i].first);
    }
    int sum = 0;
    for (int i = 0; i < k; i++) {
      sum += ch[i].second;
    }
    for (int i = 0; i < k; i++) {
      dp[u][0] = max(dp[u][0], sum - ch[i].second + max(max_val, ch[i].first));
    }
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    u++, v++;
    g[u].push_back(make_pair(v, c));
    g[v].push_back(make_pair(u, c));
  }
  dfs(1, 0);
  printf("%d\n", dp[1][0]);
}
