#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5;
long long int n, k;
vector<vector<pair<long long int, long long int>>> g;
long long int dp[N][2];
void dfs(long long int v, long long int par) {
  vector<pair<long long int, long long int>> store;
  for (pair<long long int, long long int> e : g[v]) {
    long long int u = e.first;
    if (u != par) {
      dfs(u, v);
      store.push_back({dp[u][0] + e.second, dp[u][1] + e.second});
    }
  }
  sort(store.begin(), store.end());
  reverse(store.begin(), store.end());
  long long int sum = 0;
  for (long long int i = 0; i < k - 1 and i < store.size(); ++i)
    sum += store[i].first;
  dp[v][0] = sum;
  if (store.size() <= k - 1)
    for (long long int i = 0; i < store.size(); ++i)
      dp[v][1] = max(dp[v][1], store[i].second - store[i].first + sum);
  else {
    for (long long int i = k; i < store.size(); ++i)
      dp[v][1] = max(dp[v][1], sum + store[i].second);
    sum += store[k - 1].first;
    for (long long int i = 0; i < k; ++i) {
      dp[v][1] = max(dp[v][1], store[i].second - store[i].first + sum);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  g = vector<vector<pair<long long int, long long int>>>(n + 1);
  for (long long int i = 0; i < n - 1; ++i) {
    long long int u, v, c;
    cin >> u >> v >> c;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
  }
  dfs(0, -1);
  cout << max(dp[0][0], dp[0][1]) << '\n';
  return 0;
}
