#include <bits/stdc++.h>
using namespace std;
template <class T>
T Max(const T &a, const T &b) {
  return a > b ? a : b;
}
template <class T>
T Min(const T &a, const T &b) {
  return a < b ? a : b;
}
const int SN = 200000 + 10;
int n, k, num, cnt;
int head[SN], f[SN][2];
pair<int, int> tmp[SN];
struct Edge {
  int v, next, w;
} E[SN << 1];
bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }
void Read(int &x) {
  int in = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    in = in * 10 + ch - '0';
    ch = getchar();
  }
  x = in * f;
}
void Add(int u, int v, int w) {
  E[++num].v = v;
  E[num].next = head[u];
  E[num].w = w;
  head[u] = num;
}
void dfs(int u, int fa) {
  for (int i = head[u]; i; i = E[i].next) {
    int to = E[i].v;
    if (to == fa) continue;
    dfs(to, u);
  }
  cnt = 0;
  for (int i = head[u]; i; i = E[i].next) {
    int to = E[i].v;
    if (to == fa) continue;
    tmp[++cnt] = pair<int, int>(f[to][0] + E[i].w, f[to][1] + E[i].w);
  }
  if (cnt < k) {
    for (int i = 1; i <= cnt; i++) f[u][0] += tmp[i].first;
    for (int i = 1; i <= cnt; i++)
      f[u][1] = Max(f[u][1], f[u][0] - tmp[i].first + tmp[i].second);
  } else {
    sort(tmp + 1, tmp + cnt + 1, cmp);
    for (int i = 1; i <= k - 1; i++) f[u][0] += tmp[i].first;
    for (int i = 1; i <= k - 1; i++)
      f[u][1] =
          Max(f[u][1], f[u][0] - tmp[i].first + tmp[i].second + tmp[k].first);
    for (int i = k; i <= cnt; i++)
      f[u][1] = Max(f[u][1], f[u][0] + tmp[i].second);
  }
}
int main() {
  int x, y, z;
  Read(n), Read(k);
  for (int i = 1; i < n; i++) {
    Read(x), Read(y), Read(z);
    Add(x, y, z), Add(y, x, z);
  }
  dfs(0, -1);
  printf("%d\n", Max(f[0][0], f[0][1]));
  return 0;
}
