#include <bits/stdc++.h>
using namespace std;
long long int dp[100005][2], n, k;
vector<pair<long long int, long long int> > v[100005];
void dfs(long long int cur, long long int par) {
  vector<pair<long long int, long long int> > store;
  set<long long int> ids;
  for (long long int i = 0; i < v[cur].size(); i++) {
    long long int x = v[cur][i].first;
    long long int y = v[cur][i].second;
    if (x == par) continue;
    dfs(x, cur);
    store.push_back({dp[x][0] + y, x});
  }
  sort(store.begin(), store.end());
  reverse(store.begin(), store.end());
  for (long long int i = 0; i < min(k - 1, (long long int)store.size()); i++) {
    dp[cur][0] += store[i].first;
    ids.insert(store[i].second);
  }
  long long int can;
  if (k - 1 < store.size())
    can = store[k - 1].first;
  else
    can = 0;
  for (long long int i = 0; i < v[cur].size(); i++) {
    long long int x = v[cur][i].first;
    long long int y = v[cur][i].second;
    if (x == par) continue;
    if (ids.find(x) == ids.end())
      dp[cur][1] = max(dp[cur][1], dp[cur][0] + dp[x][1] + y);
    else
      dp[cur][1] = max(dp[cur][1], dp[cur][0] - dp[x][0] + can + dp[x][1]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin.tie(0);
  ;
  long long int i, j, x, y, z;
  cin >> n >> k;
  for (i = 1; i <= n - 1; i++) {
    cin >> x >> y >> z;
    x++;
    y++;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  dfs(1, -1);
  cout << max(dp[1][0], dp[1][1]);
  return 0;
}
