#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct edge {
  int to, nxt, cost;
} e[N << 1];
int fir[N], cnt = 0;
int f[N][2];
int n, K;
inline void add(int x, int y, int c) {
  e[++cnt].to = y;
  e[cnt].nxt = fir[x];
  e[cnt].cost = c;
  fir[x] = cnt;
}
inline bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) return a.first > b.first;
  return a.second > b.second;
}
inline void dfs(int x, int pa) {
  int v, cnt, ext1 = 0, ext2 = 0, k = K;
  k--;
  vector<pair<int, int> > son;
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != pa) {
      v = e[i].to;
      dfs(v, x);
      son.push_back(make_pair(f[v][0] + e[i].cost, f[v][1] + e[i].cost));
    }
  cnt = son.size();
  sort(son.begin(), son.end(), cmp);
  if (0) {
    cout << x << "::" << endl;
    for (int i = 0; i < cnt; i++)
      cout << son[i].first << "  " << son[i].second << endl;
    cout << "|------------------|\n";
  }
  f[x][0] = 0;
  for (int i = 0; i < min(cnt, k); i++)
    f[x][0] += son[i].first, ext1 = max(ext1, son[i].second - son[i].first);
  if (cnt > k) ext1 += son[k].first;
  for (int i = k; i < cnt; i++) ext2 = max(ext2, son[i].second);
  f[x][1] = f[x][0] + max(ext1, ext2);
}
int main() {
  int x, y, c;
  scanf("%d%d", &n, &K);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &c);
    add(x, y, c);
    add(y, x, c);
  }
  dfs(0, 0);
  printf("%d\n", max(f[0][0], f[0][1]));
  return 0;
}
