#include <bits/stdc++.h>
using namespace std;
int n, k, x, y, c;
int dp[100100][2];
vector<pair<int, int> > graph[100100];
void dfs(int u, int p) {
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i].first;
    int c = graph[u][i].second;
    if (v != p) dfs(v, u);
  }
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 less<pair<int, int> > >
      pq;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i].first;
    int c = graph[u][i].second;
    if (v != p) pq.push(make_pair(dp[v][0] + c, i));
  }
  vector<int> used(graph[u].size() + 1, 0);
  for (int i = 0; i < k - 1; i++) {
    if (pq.empty()) break;
    dp[u][0] += pq.top().first;
    used[pq.top().second] = 1;
    pq.pop();
  }
  int add = 0;
  if (!pq.empty()) add = pq.top().first;
  for (int i = 0; i < graph[u].size(); i++) {
    int v = graph[u][i].first;
    if (v == p) continue;
    if (used[i])
      dp[u][1] = max(dp[u][1], add + dp[v][1] - dp[v][0]);
    else
      dp[u][1] = max(dp[u][1], dp[v][1] + graph[u][i].second);
  }
  dp[u][1] += dp[u][0];
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d %d", &x, &y, &c);
    graph[x].push_back(make_pair(y, c));
    graph[y].push_back(make_pair(x, c));
  }
  dfs(0, -1);
  printf("%d\n", max(dp[0][0], dp[0][1]));
  return 0;
}
