#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
vector<pair<int, int> > nbrs[MAX_N];
int ps[MAX_N], pcs[MAX_N], cis[MAX_N];
int dos[MAX_N], drs[MAX_N];
vector<pair<int, int> > mds[MAX_N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i < n; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    nbrs[u].push_back(pair<int, int>(v, c));
    nbrs[v].push_back(pair<int, int>(u, c));
  }
  ps[0] = -1, pcs[0] = 0;
  for (int u = 0; u >= 0;) {
    vector<pair<int, int> > &nbru = nbrs[u];
    int up = ps[u];
    if (cis[u] < nbru.size() && nbru[cis[u]].first == up) cis[u]++;
    if (cis[u] < nbru.size()) {
      pair<int, int> ue = nbru[cis[u]++];
      int v = ue.first;
      ps[v] = u;
      pcs[v] = ue.second;
      u = v;
    } else {
      vector<pair<int, int> > &mdu = mds[u];
      sort(mdu.begin(), mdu.end(), greater<pair<int, int> >());
      int l = min((int)mdu.size(), k - 1);
      drs[u] = dos[u] = 0;
      if (l > 0) {
        for (int i = 0; i < l; i++) drs[u] += mdu[i].first;
        for (int i = 0; i < mdu.size(); i++) {
          int doi = drs[u] + mdu[i].second;
          if (mdu[i].first >= mdu[l - 1].first) {
            doi -= mdu[i].first;
            if (l < mdu.size()) doi += mdu[l].first;
          }
          if (dos[u] < doi) dos[u] = doi;
        }
      } else {
        for (int i = 0; i < mdu.size(); i++)
          if (dos[u] < mdu[i].second) dos[u] = mdu[i].second;
      }
      if (up >= 0) {
        int pc = pcs[u];
        mds[up].push_back(pair<int, int>(drs[u] + pc, dos[u] + pc));
      }
      u = up;
    }
  }
  printf("%d\n", max(drs[0], dos[0]));
  return 0;
}
