#include <bits/stdc++.h>
using namespace std;
int k;
long long f[100020][2], c[100020];
bool t[100020];
vector<pair<int, long long> > e[100020];
bool cmp(const int &x, const int &y) { return f[y][1] < f[x][1]; }
void dfs(int x, int p) {
  int i;
  vector<int> v;
  long long w, m;
  v.clear();
  for (i = 0; i < (signed)e[x].size(); i++)
    if (e[x].at(i).first != p) {
      f[e[x].at(i).first][0] = f[e[x].at(i).first][1] = e[x].at(i).second;
      c[e[x].at(i).first] = e[x].at(i).second;
      dfs(e[x].at(i).first, x);
      v.push_back(e[x].at(i).first);
    }
  sort(v.begin(), v.end(), cmp);
  for (i = 0; i < (signed)v.size() && i + 1 < k; i++) {
    f[x][1] += f[v.at(i)][1];
    t[v.at(i)] = true;
  }
  for (i = m = 0; i < (signed)v.size(); i++) {
    w = f[x][1] - c[x] + max(f[v.at(i)][0], f[v.at(i)][1]);
    if (t[v.at(i)]) {
      w -= f[v.at(i)][1];
      if (k <= (signed)v.size()) w += f[v.at(k - 1)][1];
    }
    m = max(m, w);
  }
  f[x][0] += m;
  return;
}
int main(void) {
  int n;
  int i;
  int u, v;
  long long c;
  scanf("%d %d", &n, &k);
  for (i = 1; i < n; i++) {
    scanf("%d %d %I64d", &u, &v, &c);
    e[u].push_back(make_pair(v, c));
    e[v].push_back(make_pair(u, c));
  }
  dfs(0, -1);
  printf("%I64d\n", max(f[0][0], f[0][1]));
  return 0;
}
