#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 1e6 + 7;
const double eps = 0.00000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
vector<pair<long long, long long> > edge[maxn];
long long t, i, k;
long long ans[maxn], sum[maxn];
void dfs(int x, int from) {
  long long i, tot;
  vector<pair<long long, long long> > V;
  for (i = 0; i < edge[x].size(); i++)
    if (edge[x][i].first != from) {
      dfs(edge[x][i].first, x);
      V.push_back(make_pair(sum[edge[x][i].first] + edge[x][i].second,
                            ans[edge[x][i].first] + edge[x][i].second));
    }
  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());
  tot = V.size();
  tot = min(tot, k - 1);
  for (i = 0; i < tot; i++) sum[x] += V[i].first;
  for (i = 0; i < tot; i++) {
    if (V.size() >= k)
      ans[x] = max(ans[x], V[i].second - V[i].first + V[k - 1].first);
    else
      ans[x] = max(ans[x], V[i].second - V[i].first);
  }
  for (i = tot; i < V.size(); i++) ans[x] = max(ans[x], V[i].second);
  ans[x] += sum[x];
}
long long n, u, v, c;
int main() {
  scanf("%I64d%I64d", &n, &k);
  for (int i = 0; i < n - 1; i++) {
    scanf("%I64d%I64d%I64d", &u, &v, &c);
    edge[u].push_back(make_pair(v, c));
    edge[v].push_back(make_pair(u, c));
  }
  dfs(0, -1);
  printf("%I64d\n", ans[0]);
  return 0;
}
