#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g[100010];
int N, K, in[100010], dp[100010][2];
vector<pair<int, int> > num[100010];
void dfs(int u, int fa) {
  for (pair<int, int>& e : g[u]) {
    int v = e.first, cost = e.second;
    if (v == fa) continue;
    dfs(v, u);
    num[u].push_back(pair<int, int>(dp[v][1] + cost, v));
  }
  sort(num[u].begin(), num[u].end(), greater<pair<int, int> >());
  dp[u][1] = 0;
  int sum = 0;
  for (size_t i = 0; (int)i < K - 1 && i < num[u].size(); i++) {
    dp[u][1] += num[u][i].first;
    sum += num[u][i].first;
  }
  if ((int)num[u].size() <= K - 1) {
    for (size_t i = 0; i < num[u].size(); i++) {
      int v = num[u][i].second;
      dp[u][0] = max(dp[u][0], sum - dp[v][1] + dp[v][0]);
    }
  } else {
    for (size_t i = 0; i < (int)K - 1; i++) {
      int v = num[u][i].second;
      dp[u][0] = max(dp[u][0], sum + num[u][K - 1].first - dp[v][1] + dp[v][0]);
    }
    for (size_t i = K - 1; i < num[u].size(); i++) {
      int v = num[u][i].second;
      dp[u][0] = max(dp[u][0], sum + num[u][i].first - dp[v][1] + dp[v][0]);
    }
  }
}
int main() {
  int u, v, c;
  scanf("%d%d", &N, &K);
  for (int i = 1; i < N; i++) {
    scanf("%d%d%d", &u, &v, &c);
    g[u].push_back(pair<int, int>(v, c));
    g[v].push_back(pair<int, int>(u, c));
  }
  dfs(0, -1);
  printf("%d\n", max(dp[0][0], dp[0][1]));
  return 0;
}
