#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<pair<int, int> > V[N], t[N];
int M[N], D[N], H[N], n, k, i, x, y, z;
void f(int x) {
  int i, y;
  H[x] = 1;
  for (i = 0; i < V[x].size(); i++)
    if (!H[y = V[x][i].first]) {
      f(y);
      t[x].push_back(make_pair((M[y] += V[x][i].second), y));
      D[y] += V[x][i].second;
    }
  sort(t[x].begin(), t[x].end());
  reverse(t[x].begin(), t[x].end());
  y = min(k - 1, (int)t[x].size());
  for (i = 0; i < y; i++) M[x] += t[x][i].first;
  for (; i < t[x].size(); i++) D[x] = max(D[x], D[t[x][i].second]);
  D[x] += M[x];
  for (i = 0; i < y; i++)
    D[x] = max(D[x], D[t[x][i].second] + M[x] - t[x][i].first +
                         (t[x].size() >= k ? t[x][y].first : 0));
}
int main() {
  cin >> n >> k;
  for (i = n; --i;) {
    cin >> x >> y >> z;
    V[x].push_back(make_pair(y, z));
    V[y].push_back(make_pair(x, z));
  }
  f(0);
  cout << D[0];
  return 0;
}
