#include <bits/stdc++.h>
using namespace std;
int F[100005], G[100005], K;
vector<pair<int, int>> E[100005];
void go(int x, int p = -1) {
  vector<pair<int, int>> g;
  for (auto e : E[x]) {
    int y = e.first, c = e.second;
    if (y == p) continue;
    go(y, x);
    g.push_back({G[y] + c, F[y] + c});
  }
  sort(g.begin(), g.end(), greater<pair<int, int>>());
  int bestdelta = 0;
  for (int i = 0; i < min(K - 1, (int)g.size()); ++i) {
    G[x] += g[i].first;
    int replace = g.size() >= K ? g[K - 1].first : 0;
    bestdelta = max(bestdelta, g[i].second - g[i].first + replace);
  }
  for (int i = K - 1; i < g.size(); ++i)
    bestdelta = max(bestdelta, g[i].second);
  F[x] = G[x] + bestdelta;
}
int main() {
  int N;
  scanf("%d%d", &N, &K);
  for (int i = 1; i < N; ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    E[u].push_back({v, c});
    E[v].push_back({u, c});
  }
  go(0);
  printf("%d\n", F[0]);
}
