#include <bits/stdc++.h>
using namespace std;
struct node {
  int v, next, c;
  node() {}
  node(int v, int next, int c) {
    this->v = v;
    this->next = next;
    this->c = c;
  }
} eg[100004 * 2];
int head[100004], tot = 0, n, k;
int dp[100004][2];
void add(int x, int y, int z) {
  eg[tot] = node(y, head[x], z);
  head[x] = tot++;
}
void dfs(int p, int fa) {
  vector<pair<int, int> > v;
  for (int i = head[p]; ~i; i = eg[i].next) {
    int x = eg[i].v;
    if (x == fa) continue;
    dfs(x, p);
    v.push_back(make_pair(dp[x][0] + eg[i].c, dp[x][1] + eg[i].c));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int m = v.size(), sum = 0;
  for (int i = 0; i < k - 1 && i < m; i++) sum += v[i].first;
  dp[p][0] = sum;
  if (m < k) {
    for (int i = 0; i < m; i++)
      dp[p][1] = max(dp[p][1], sum + v[i].second - v[i].first);
    return;
  }
  for (int i = k; i < m; i++) dp[p][1] = max(dp[p][1], sum + v[i].second);
  sum += v[k - 1].first;
  for (int i = 0; i < k; i++)
    dp[p][1] = max(dp[p][1], sum + v[i].second - v[i].first);
}
int main() {
  scanf("%d%d", &n, &k);
  memset(head, -1, sizeof(head));
  for (int i = 1; i < n; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    add(x, y, z);
    add(y, x, z);
  }
  dfs(0, -1);
  printf("%d\n", max(dp[0][0], dp[0][1]));
  return 0;
}
