#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y) {
    if (y & 1) res = (res * x) % 1000000007;
    y = y / 2, x = (x * x) % 1000000007;
  }
  return res % 1000000007;
}
long long int dp[100001][2] = {{0}};
vector<vector<pair<long long int, long long int> > > arr(100001);
long long int n, k;
void dfs(long long int u, long long int par) {
  long long int cnt = 0;
  vector<pair<long long int, long long int> > lol;
  long long int i;
  for (i = 0; i < arr[u].size(); i++) {
    if (arr[u][i].first != par) {
      dfs(arr[u][i].first, u);
      dp[arr[u][i].first][0] += arr[u][i].second;
      dp[arr[u][i].first][1] += arr[u][i].second;
      cnt++;
    }
  }
  if (cnt < k) {
    cnt = 0;
    for (i = 0; i < arr[u].size(); i++) {
      if (arr[u][i].first != par)
        dp[u][1] += dp[arr[u][i].first][1], cnt += dp[arr[u][i].first][1];
    }
    long long int tot = 0;
    for (i = 0; i < arr[u].size(); i++) {
      if (arr[u][i].first != par) {
        tot = max(tot, cnt - dp[arr[u][i].first][1] + dp[arr[u][i].first][0]);
      }
    }
    dp[u][0] = tot;
    return;
  }
  for (i = 0; i < arr[u].size(); i++) {
    if (arr[u][i].first != par)
      lol.push_back(make_pair(dp[arr[u][i].first][1], arr[u][i].first));
  }
  sort(lol.begin(), lol.end());
  reverse(lol.begin(), lol.end());
  set<long long int> haha;
  long long int ans = 0;
  for (i = 0; i < k - 1; i++) ans += lol[i].first, haha.insert(lol[i].second);
  dp[u][1] = ans;
  ans += lol[k - 1].first;
  haha.insert(lol[k - 1].second);
  long long int tot = 0;
  for (i = 0; i < arr[u].size(); i++) {
    if (arr[u][i].first != par) {
      if (haha.find(arr[u][i].first) != haha.end()) {
        tot = max(tot, ans - dp[arr[u][i].first][1] + dp[arr[u][i].first][0]);
      } else
        tot = max(tot, dp[arr[u][i].first][0] + dp[u][1]);
    }
  }
  dp[u][0] = tot;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  long long int i;
  for (i = 0; i < n - 1; i++) {
    long long int u, v, c;
    cin >> u >> v >> c;
    arr[u].push_back(make_pair(v, c));
    arr[v].push_back(make_pair(u, c));
  }
  dfs(0, -1);
  cout << dp[0][0];
}
