#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<long long, int> > G[100001];
bool flag[100001];
vector<long long> res;
long long dp[100001][2];
long long dfs(int v, int f, int p) {
  if (dp[v][f] != -1) return dp[v][f];
  vector<pair<long long, int> > ca;
  for (int i = 0; i < G[v].size(); i++) {
    if (p != G[v][i].first) {
      ca.push_back(
          pair<long long, int>(dfs(G[v][i].first, 1, v) + G[v][i].second, i));
    }
  }
  if (ca.size() == 0) return (dp[v][f] = 0);
  sort(ca.begin(), ca.end());
  reverse(ca.begin(), ca.end());
  long long sum = 0;
  for (int i = 0; i < min(k - 1, (int)ca.size()); i++) {
    sum += ca[i].first;
  }
  if (f == 1) return (dp[v][f] = sum);
  long long res = 0;
  for (int i = 0; i < ca.size(); i++) {
    long long val =
        dfs(G[v][ca[i].second].first, 0, v) + G[v][ca[i].second].second;
    if (i < k) {
      if (ca.size() >= k)
        res = max(res, sum + val - ca[i].first + ca[k - 1].first);
      else
        res = max(res, sum + val - ca[i].first);
    } else {
      res = max(res, sum + val);
    }
  }
  return (dp[v][f] = res);
}
int main(void) {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n - 1; i++) {
    int f, t, c;
    scanf("%d%d%d", &f, &t, &c);
    G[f].push_back(pair<long long, int>(t, c));
    G[t].push_back(pair<long long, int>(f, c));
  }
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", dfs(0, 0, -1));
  return 0;
}
