#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int head[maxn], to[maxn << 1], w[maxn << 1], nxt[maxn << 1], cnt;
void add(int u, int v, int c) {
  ++cnt, to[cnt] = v, w[cnt] = c, nxt[cnt] = head[u], head[u] = cnt;
  ++cnt, to[cnt] = u, w[cnt] = c, nxt[cnt] = head[v], head[v] = cnt;
}
long long dp[maxn][2];
struct node {
  int now;
  long long d0, d1;
  node(int now = 0, long long d0 = 0, long long d1 = 0)
      : now(now), d0(d0), d1(d1) {}
};
bool cmp(node a, node b) { return a.d1 > b.d1; }
int n, k;
void dfs1(int u, int pre, int last) {
  vector<node> e;
  e.clear();
  dp[u][0] = dp[u][1] = last;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == pre) continue;
    dfs1(v, u, w[i]);
    e.push_back(node(v, dp[v][0], dp[v][1]));
  }
  long long ans = 0, maxs = 0;
  sort(e.begin(), e.end(), cmp);
  for (int i = 0; i < k - 1 && i < e.size(); i++) dp[u][1] += e[i].d1;
  for (int i = 0; i < k && i < e.size(); i++) ans += e[i].d1;
  for (int i = 0; i < e.size(); i++) {
    if (i < k)
      maxs = max(maxs, ans - e[i].d1 + e[i].d0);
    else
      maxs = max(maxs, ans - e[k - 1].d1 + e[i].d0);
  }
  dp[u][0] += maxs;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    add(u, v, c);
  }
  dfs1(0, -1, 0);
  printf("%lld\n", dp[0][0]);
  return 0;
}
