#include <bits/stdc++.h>
using namespace std;
inline int r() {
  int k = 1, s = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') k = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    s = s * 10 + c - '0';
    c = getchar();
  }
  return s * k;
}
struct node {
  int to, next, dis;
} a[250001];
struct nood {
  int w, bh;
} s[200001];
int n, k, cnt, head[200001], f[200001][2], size[200001], sum[200001];
void add_edge(int from, int to, int dis) {
  a[++cnt].to = to;
  a[cnt].next = head[from];
  a[cnt].dis = dis;
  head[from] = cnt;
}
bool cmp(nood x, nood y) { return x.w > y.w; }
void dfs(int u, int fa) {
  for (int i = head[u]; i; i = a[i].next) {
    int v = a[i].to;
    if (v != fa) dfs(v, u);
  }
  int t = 0;
  for (int i = head[u]; i; i = a[i].next) {
    int v = a[i].to;
    if (v != fa) {
      f[v][1] += a[i].dis;
      f[v][0] += a[i].dis;
      s[++t].w = f[v][0];
      s[t].bh = v;
    }
  }
  sort(s + 1, s + t + 1, cmp);
  for (int i = 1; i <= t; i++) sum[i] = sum[i - 1] + s[i].w;
  f[u][0] = sum[min(k - 1, t)];
  for (int i = 1; i <= min(k, t); i++) {
    f[u][1] = max(f[u][1], sum[min(k, t)] - s[i].w + f[s[i].bh][1]);
  }
  for (int i = k + 1; i <= t; i++)
    f[u][1] = max(f[u][1], sum[k - 1] + f[s[i].bh][1]);
}
int main() {
  int x, y, z;
  n = r();
  k = r();
  for (int i = 1; i < n; i++) {
    x = r();
    y = r();
    z = r();
    x++;
    y++;
    add_edge(x, y, z);
    add_edge(y, x, z);
  }
  dfs(1, 0);
  cout << f[1][1];
  return 0;
}
