#include <bits/stdc++.h>
using namespace std;
void IO() {
  srand(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
map<pair<int, int>, int> cost;
vector<vector<int> > g;
int n, k;
vector<int> visited;
const int maxn = 1e5 + 1;
int ans[maxn][2];
void dfs(int s, int p) {
  visited[s] = 1;
  vector<pair<int, int> > res;
  for (auto it : g[s]) {
    if (!visited[it]) {
      dfs(it, s);
      res.push_back({ans[it][0] + cost[{s, it}], it});
    }
  }
  sort(res.begin(), res.end(), greater<pair<int, int> >());
  int lim = (int)(res.size());
  int sum = 0;
  for (int i = 0; i < min(k, lim); i++) {
    sum += res[i].first;
  }
  int sum2 = 0;
  for (int i = 0; i < min(k - 1, lim); i++) sum2 += res[i].first;
  int temp = 0;
  for (int i = 0; i < lim; i++) {
    if (i < k) {
      temp = max(temp, sum - res[i].first + cost[{s, res[i].second}] +
                           ans[res[i].second][1]);
    } else {
      temp = max(temp, sum2 + cost[{s, res[i].second}] + ans[res[i].second][1]);
    }
  }
  ans[s][0] = sum2;
  ans[s][1] = temp;
}
int main() {
  IO();
  cin >> n >> k;
  int a, b, c;
  g.resize(n);
  visited.resize(n, 0);
  for (__typeof(n - 1) i = (0) - ((0) > (n - 1));
       i != (n - 1) - ((0) > (n - 1)); i += 1 - 2 * ((0) > (n - 1))) {
    cin >> a >> b >> c;
    g[a].push_back(b);
    g[b].push_back(a);
    cost[{a, b}] = c;
    cost[{b, a}] = c;
  }
  dfs(0, -1);
  cout << max(ans[0][1], ans[0][0]);
  return 0;
}
