#include <bits/stdc++.h>
using namespace std;
long long n, u, v, c, k;
vector<pair<long long, long long> > G[100007];
bitset<100007> vis;
long long dp[100007];
long long stay[100007];
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
  return dp[a.first] + a.second > dp[b.first] + b.second;
}
void dfs(long long v) {
  vis[v] = 1;
  vector<pair<long long, long long> > s;
  for (auto it : G[v]) {
    if (!vis[it.first]) {
      dfs(it.first);
      s.push_back(it);
    }
  }
  sort((s).begin(), (s).end(), comp);
  if (s.size() >= k) {
    long long sum = 0;
    for (long long i = 0; i < k - 1; ++i) {
      sum += dp[s[i].first] + s[i].second;
    }
    dp[v] = stay[v] = sum;
    for (long long i = 0; i < k - 1; ++i) {
      stay[v] = max(stay[v], sum - dp[s[i].first] + stay[s[i].first] +
                                 dp[s[k - 1].first] + s[k - 1].second);
    }
    for (long long i = k - 1; i < s.size(); ++i) {
      stay[v] = max(stay[v], sum + stay[s[i].first] + s[i].second);
    }
  } else {
    long long sum = 0;
    for (long long i = 0; i < s.size(); ++i) {
      sum += dp[s[i].first] + s[i].second;
    }
    dp[v] = stay[v] = sum;
    for (long long i = 0; i < s.size(); ++i) {
      stay[v] = max(stay[v], sum - dp[s[i].first] + stay[s[i].first]);
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> k;
  for (long long i = 1; i < n; ++i) {
    cin >> u >> v >> c;
    G[u].emplace_back(v, c);
    G[v].emplace_back(u, c);
  }
  dfs(0);
  cout << stay[0];
}
