#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-') f = -1;
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int n, k, tot, h[100005], f[100005][2], st[100005], sum[100005];
struct AB {
  int a, b, c, n;
} d[200005];
void charu(int a, int b, int c) {
  d[++tot].a = a, d[tot].b = b, d[tot].c = c, d[tot].n = h[a], h[a] = tot;
}
bool cmp(int a, int b) { return f[a][1] > f[b][1]; }
void dfs(int u, int fa) {
  for (int k = h[u]; k; k = d[k].n) {
    int v = d[k].b, c = d[k].c;
    if (v == fa) continue;
    dfs(v, u);
    f[v][0] += c, f[v][1] += c;
  }
  int cnt = 0;
  for (int k = h[u]; k; k = d[k].n) {
    int v = d[k].b, c = d[k].c;
    if (v == fa) continue;
    st[++cnt] = v;
  }
  sort(st + 1, st + cnt + 1, cmp);
  for (int i = 1; i <= cnt; i++) sum[i] = sum[i - 1] + f[st[i]][1];
  f[u][1] = sum[min(k - 1, cnt)];
  int now = 0;
  for (int i = 1; i <= cnt; i++) {
    if (i < k)
      f[u][0] = max(f[u][0], sum[min(k, cnt)] - f[st[i]][1] + f[st[i]][0]);
    else
      f[u][0] = max(f[u][0], sum[min(k - 1, cnt)] + f[st[i]][0]);
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i < n; i++) {
    int a = read() + 1, b = read() + 1, c = read();
    charu(a, b, c), charu(b, a, c);
  }
  dfs(1, 1);
  cout << f[1][0];
}
