#include <bits/stdc++.h>
using namespace std;
pair<int, int> f[100005];
int n, k;
bool Free[100005][2];
vector<pair<int, int> > g[100005];
void DFS(int u, int p, int type) {
  if (Free[u][type] == true) return;
  Free[u][type] = true;
  int tmp = k;
  vector<pair<int, int> > V;
  V.clear();
  for (int i = 0; i < g[u].size(); i++)
    if (g[u][i].first != p) {
      DFS(g[u][i].first, u, 0);
      DFS(g[u][i].first, u, 1);
      V.push_back(pair<int, int>(f[g[u][i].first].first + g[u][i].second,
                                 f[g[u][i].first].second + g[u][i].second));
    }
  sort(V.begin(), V.end(), greater<pair<int, int> >());
  int res = 0;
  if (k > V.size()) {
    for (int i = 0; i < V.size(); i++) res += V[i].first;
    if (type == 1) {
      int maxn = res;
      for (int i = 0; i < V.size(); i++)
        maxn = max(maxn, res - V[i].first + V[i].second);
      res = maxn;
    }
  } else {
    for (int i = 0; i < k - 1; i++) res += V[i].first;
    if (type == 1) {
      int maxn = res;
      for (int i = 0; i < k - 1; i++)
        maxn = max(maxn, res - V[i].first + V[i].second + V[k - 1].first);
      for (int i = k - 1; i < V.size(); i++)
        maxn = max(maxn, res + V[i].second);
      res = maxn;
    }
  }
  if (type == 0)
    f[u].first = res;
  else
    f[u].second = res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  int u, v, w;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    g[u].push_back(pair<int, int>(v, w));
    g[v].push_back(pair<int, int>(u, w));
  }
  DFS(0, -1, 0);
  DFS(0, -1, 1);
  cout << max(f[0].first, f[0].second);
}
