#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int fst[N], nxt[2 * N], v[2 * N], w[2 * N], cnt;
int dp[N][2], ans, k;
void add(int x, int y, int z) {
  v[++cnt] = y, w[cnt] = z;
  nxt[cnt] = fst[x], fst[x] = cnt;
}
void dfs(int x, int fa) {
  int tmp = 0, qwq, qaq;
  priority_queue<pair<int, int> > q;
  for (int i = fst[x]; i; i = nxt[i]) {
    int y = v[i];
    if (y == fa) continue;
    dfs(y, x);
    q.push(make_pair(dp[y][0] + w[i], y));
  }
  for (int i = 1; i < k && !q.empty(); i++) {
    dp[x][0] += q.top().first;
    qwq = q.top().second;
    tmp = max(tmp, dp[qwq][1] - dp[qwq][0]);
    q.pop();
  }
  if (q.empty()) {
    dp[x][1] = dp[x][0] + tmp;
    return;
  }
  qwq = q.top().second;
  tmp = max(tmp, dp[qwq][1] - dp[qwq][0]) + q.top().first;
  while (!q.empty()) {
    qwq = q.top().second, qaq = q.top().first;
    q.pop();
    tmp = max(tmp, qaq + dp[qwq][1] - dp[qwq][0]);
  }
  dp[x][1] = dp[x][0] + tmp;
}
int main() {
  int n, x, y, z;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    add(x, y, z), add(y, x, z);
  }
  dfs(0, -1);
  printf("%d", dp[0][1]);
  return 0;
}
