#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 1000;
struct Edge {
  int next, to, w;
} e[2 * MAXN];
int p[MAXN], l = 2;
void addE(int a, int b, int c) {
  e[l].next = p[a];
  e[l].to = b, e[l].w = c;
  p[a] = l++;
}
int n, m;
int f[MAXN], g[MAXN];
bool cmp_f(int x, int y) { return f[x] > f[y]; }
void calc(int x, int fa) {
  static int list[MAXN];
  int tot = 0;
  for (int i = p[x]; i; i = e[i].next)
    if (e[i].to != fa) list[++tot] = e[i].to;
  sort(list + 1, list + tot + 1, cmp_f);
  int sum = 0;
  for (int i = 1; i <= m - 1 && i <= tot; i++) sum += f[list[i]];
  f[x] = sum;
  for (int i = m; i <= tot; i++) g[x] = max(g[x], sum + g[list[i]]);
  if (m <= tot) sum += f[list[m]];
  for (int i = 1; i <= m - 1 && i <= tot; i++)
    g[x] = max(g[x], sum - f[list[i]] + g[list[i]]);
}
void dfs(int x, int fa, int w) {
  for (int i = p[x]; i; i = e[i].next)
    if (e[i].to != fa) dfs(e[i].to, x, e[i].w);
  calc(x, fa);
  f[x] += w, g[x] += w;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n - 1; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x += 1, y += 1;
    addE(x, y, z), addE(y, x, z);
  }
  dfs(1, 0, 0);
  int ans = max(f[1], g[1]);
  cout << ans << endl;
  return 0;
}
