#include <bits/stdc++.h>
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long add(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a + b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long mul(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a * b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long pow_mod(long long a, long long b, long long m = MOD) {
  long long res = 1LL;
  a = a % m;
  while (b) {
    if (b & 1) res = mul(res, a, m);
    b >>= 1;
    a = mul(a, a, m);
  }
  return res;
}
long long fastexp(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a;
    b >>= 1;
    a *= a;
  }
  return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtendido(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
int modInverso(int a, int m) {
  int x, y;
  int g = gcdExtendido(a, m, &x, &y);
  if (g != 1)
    return -1;
  else
    return (x % m + m) % m;
}
const int N = 100000 + 5;
int n, k;
vector<int> G[N], W[N];
long long memo[N][2];
bool vis[N][2];
long long DP(int u, int mode, int p = -1) {
  if (vis[u][mode]) return memo[u][mode];
  vis[u][mode] = true;
  long long ans = 0LL;
  if (mode) {
    vector<long long> a;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      int w = W[u][i];
      if (v == p) continue;
      a.emplace_back(w + DP(v, 1, u));
    }
    sort((a).begin(), (a).end(), greater<long long>());
    for (int i = 0; i + 1 < k and i < a.size(); i++) {
      ans += a[i];
    }
  } else {
    int limit = k - 1;
    vector<pair<long long, int> > a;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      int w = W[u][i];
      if (v == p) continue;
      a.emplace_back(make_pair(w + DP(v, 1, u), i));
    }
    sort((a).begin(), (a).end(), greater<pair<long long, int> >());
    long long valis = 0LL;
    long long valno = 0LL;
    for (int i = 0; i < limit and i < a.size(); i++) {
      valno += a[i].first;
      valis += a[i].first;
    }
    if (limit + 1 <= a.size()) {
      valis += a[limit].first;
    }
    vector<int> id(G[u].size(), 0);
    for (int i = 0; i < a.size(); i++) {
      id[a[i].second] = i;
    }
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      int w = W[u][i];
      if (v == p) continue;
      long long cand = DP(v, 0, u) + w;
      if (id[i] < limit) {
        cand += valis - DP(v, 1, u) - w;
      } else {
        cand += valno;
      }
      ans = max(ans, cand);
    }
  }
  return memo[u][mode] = ans;
}
int main() {
  scanf("%d %d", &(n), &(k));
  int a, b, w;
  for (int i = 1; i < n; i++) {
    scanf("%d %d %d", &(a), &(b), &(w));
    G[a].emplace_back(b);
    G[b].emplace_back(a);
    W[a].emplace_back(w);
    W[b].emplace_back(w);
  }
  long long ans = max(DP(0, 0), DP(0, 1));
  cout << ans << endl;
  return 0;
}
