#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int maxn = 100005;
int n, k, to[maxn << 1], nx[maxn << 1], len[maxn << 1], hd[maxn], cnt;
void addedge(int u, int v, int w) {
  to[cnt] = v;
  nx[cnt] = hd[u];
  len[cnt] = w;
  hd[u] = cnt++;
}
int f[maxn][2], ct;
pair<int, int> t[maxn];
bool cmp(const pair<int, int>& a, const pair<int, int>& b) { return a > b; }
void dfs(int u, int fa) {
  for (int i = hd[u]; ~i; i = nx[i]) {
    int v = to[i];
    if (v == fa) continue;
    dfs(v, u);
  }
  ct = 0;
  for (int i = hd[u]; ~i; i = nx[i]) {
    int v = to[i];
    if (v == fa) continue;
    t[++ct] = make_pair(f[v][0] + len[i], f[v][1] + len[i]);
  }
  sort(t + 1, t + ct + 1, cmp);
  for (int i = int(1); i <= (int)(min(ct, k - 1)); i++)
    f[u][0] += t[i].first, f[u][1] += t[i].first;
  if (ct >= k) f[u][1] += t[k].first;
  int mx = 0;
  for (int i = int(1); i <= (int)(min(ct, k)); i++)
    mx = max(mx, t[i].second - t[i].first);
  for (int i = int(k + 1); i <= (int)(ct); i++)
    mx = max(mx, t[i].second - t[k].first);
  f[u][1] += mx;
}
int main() {
  n = rd(), k = rd();
  memset(hd, -1, sizeof hd);
  cnt = 0;
  for (int i = int(1); i <= (int)(n - 1); i++) {
    int u = rd(), v = rd(), w = rd();
    addedge(u, v, w), addedge(v, u, w);
  }
  dfs(0, -1);
  rt(f[0][1]), putchar('\n');
}
