#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<int, int> > g[100005];
int dp[100005][2];
void dfs(int u, int p = -1) {
  vector<int> child;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first, uv = g[u][i].second;
    if (v == p) continue;
    child.push_back(v);
    dfs(v, u);
    dp[v][1] += uv;
  }
  sort(child.begin(), child.end(),
       [](int x, int y) { return dp[x][1] > dp[y][1]; });
  int t = min(k - 1, (int)child.size());
  set<int> chosen;
  for (int i = 0; i < t; i++)
    dp[u][1] += dp[child[i]][1], chosen.insert(child[i]);
  int last = t < child.size() ? dp[child[t]][1] : 0;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i].first, uv = g[u][i].second;
    if (v == p) continue;
    if (chosen.find(v) == chosen.end())
      dp[u][0] = max(dp[u][0], dp[u][1] + dp[v][0] + uv);
    else
      dp[u][0] = max(dp[u][0], dp[u][1] - dp[v][1] + last + dp[v][0] + uv);
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n - 1; i++) {
    int u, v, uv;
    scanf("%d %d %d", &u, &v, &uv);
    g[u].push_back(pair<int, int>(v, uv));
    g[v].push_back(pair<int, int>(u, uv));
  }
  dfs(0);
  printf("%d\n", max(dp[0][0], dp[0][1]));
}
