#include <bits/stdc++.h>
using namespace std;
int asd[100008][2];
vector<int> v[100008];
vector<int> cost[100008];
vector<pair<int, int> > p[100008];
int n, k;
bool used[100008];
int qwe[100008];
bool is[100008];
int dfs(int x, int y) {
  if (asd[x][y] != -1) {
    return asd[x][y];
  }
  asd[x][y] = 0;
  if (y == 1) {
    is[x] = 1;
    vector<int> q;
    q.clear();
    int res = 0;
    for (int i = 0; i < v[x].size(); i++) {
      if (is[v[x][i]]) continue;
      q.push_back(dfs(v[x][i], 1) + cost[x][i]);
    }
    sort(q.begin(), q.end());
    reverse(q.begin(), q.end());
    for (int i = 0; i < min((int)q.size(), k - 1); i++) {
      res += q[i];
    }
    asd[x][y] = res;
    is[x] = 0;
    return res;
  }
  is[x] = 1;
  vector<pair<int, int> > q;
  q.clear();
  int res = 0;
  for (int i = 0; i < v[x].size(); i++) {
    if (is[v[x][i]]) continue;
    used[v[x][i]] = 0;
    q.push_back(make_pair(dfs(v[x][i], 1) + cost[x][i], v[x][i]));
    qwe[v[x][i]] = dfs(v[x][i], 1) + cost[x][i];
  }
  sort(q.begin(), q.end());
  reverse(q.begin(), q.end());
  for (int i = 0; i < min((int)q.size(), k - 1); i++) {
    res += q[i].first;
    used[q[i].second] = 1;
  }
  asd[x][y] = res;
  for (int i = 0; i < v[x].size(); i++) {
    if (is[v[x][i]]) continue;
    int uiop = max(dfs(v[x][i], 1), dfs(v[x][i], 0)) + cost[x][i] + res;
    if (used[v[x][i]] and q.size() >= k) uiop = uiop + q[k - 1].first;
    if (used[v[x][i]]) uiop = uiop - qwe[v[x][i]];
    asd[x][y] = max(asd[x][y], uiop);
  }
  is[x] = 0;
  return asd[x][y];
}
int main() {
  memset(asd, -1, sizeof(asd));
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    v[a].push_back(b);
    cost[a].push_back(c);
    swap(a, b);
    v[a].push_back(b);
    cost[a].push_back(c);
  }
  cout << max(dfs(0, 1), dfs(0, 0)) << endl;
  return 0;
}
