#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, k, cnt, h[100005], f[100005][2], ind[100005];
struct Edge {
  int to, next, w;
} a[100005 * 2];
void Add(int x, int y, int z) {
  cnt++;
  a[cnt].to = y;
  a[cnt].w = z;
  a[cnt].next = h[x];
  h[x] = cnt;
}
struct Com {
  int val, id, w;
  bool operator<(const Com &t) const { return val > t.val; }
};
vector<Com> vec[100005];
void dfs(int x, int fa) {
  int tot = 0, maxx = 0;
  for (int i = h[x]; i; i = a[i].next) {
    int y = a[i].to;
    if (y == fa) continue;
    dfs(y, x);
    tot++;
    vec[x].push_back((Com){a[i].w + f[y][0], y, a[i].w});
  }
  sort(vec[x].begin(), vec[x].end());
  for (int i = 1; i <= min(tot, k - 1); i++) {
    f[x][0] += vec[x][i - 1].val, f[x][1] += vec[x][i - 1].val;
  }
  if (tot >= k) {
    f[x][1] += vec[x][k - 1].val;
    for (int i = 1; i <= k; i++)
      maxx = max(maxx,
                 vec[x][i - 1].w + f[vec[x][i - 1].id][1] - vec[x][i - 1].val);
    for (int i = k + 1; i <= tot; i++)
      maxx = max(maxx,
                 vec[x][i - 1].w + f[vec[x][i - 1].id][1] - vec[x][k - 1].val);
  } else
    for (int i = 1; i <= min(tot, k - 1); i++)
      maxx = max(maxx,
                 vec[x][i - 1].w + f[vec[x][i - 1].id][1] - vec[x][i - 1].val);
  f[x][1] += maxx;
}
int main() {
  int tot = 0;
  n = read();
  k = read();
  for (int i = 1; i < n; i++) {
    int x = read() + 1, y = read() + 1, z = read();
    Add(x, y, z);
    Add(y, x, z);
    tot += z;
  }
  dfs(1, 0);
  cout << max(f[1][0], f[1][1]);
}
