#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to;
  int d;
  Edge(int _to = 0, int _d = 0) : to(_to), d(_d) {}
};
struct Hs {
  long long v;
  long long e;
  Hs(long long _v = 0, long long _e = 0) : v(_v), e(_e) {}
};
bool cmp(Hs p1, Hs p2) { return p1.v < p2.v; }
vector<Edge> e[100100];
int n, k;
int a, b, c;
bool used[100100];
Hs dfs(int v, int d) {
  used[v] = 1;
  vector<Hs> hp;
  for (int i = 0; i < e[v].size(); ++i) {
    if (!used[e[v][i].to]) {
      hp.push_back(dfs(e[v][i].to, e[v][i].d));
      push_heap(hp.begin(), hp.end(), cmp);
    }
  }
  Hs r(d, 0);
  for (int i = 0; !hp.empty() && i < k - 1; ++i) {
    pop_heap(hp.begin(), hp.end(), cmp);
    r.v += hp.back().v;
    if (hp.back().e > r.e) r.e = hp.back().e;
    hp.pop_back();
  }
  if (!hp.empty()) {
    if (hp.front().e > r.e) r.e = hp.front().e;
    r.e += hp.front().v;
  }
  while (!hp.empty()) {
    pop_heap(hp.begin(), hp.end(), cmp);
    if (hp.back().e + hp.back().v > r.e) r.e = hp.back().e + hp.back().v;
    hp.pop_back();
  }
  return r;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n - 1; ++i) {
    cin >> a >> b >> c;
    e[a].push_back(Edge(b, c));
    e[b].push_back(Edge(a, c));
  }
  Hs r = dfs(0, 0);
  cout << r.v + r.e << "\n";
  return 0;
}
