#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) x = x * 10 + (c & 15), c = getchar();
}
int n, k, head[100001], cnt, f[100001][2];
bool vis[100001];
struct Edge {
  int a, b, c;
} e[100001 << 1];
inline void add(int a, int b, int c) {
  e[++cnt].a = head[a], e[cnt].b = b, e[cnt].c = c, head[a] = cnt;
}
inline void dfs1(int x, int y) {
  int k = 0;
  for (int i = head[x]; i; i = e[i].a) {
    if (e[i].b == y) continue;
    f[e[i].b][0] = e[i].c;
    dfs1(e[i].b, x);
    if (f[e[i].b][0] > f[k][0]) k = e[i].b;
  }
  f[x][0] += f[k][0];
}
struct sb1 {
  int x, id;
  inline sb1(int x = 0, int id = 0) : x(x), id(id) {}
};
inline bool operator<(const sb1 &a, const sb1 &b) { return a.x < b.x; }
struct sb2 {
  int x, id;
  inline sb2(int x = 0, int id = 0) : x(x), id(id) {}
};
inline bool operator<(const sb2 &a, const sb2 &b) { return a.x > b.x; }
inline void dfs(int x, int y) {
  priority_queue<sb1> q1;
  priority_queue<sb2> q2;
  int cnt = 0, val = f[x][0];
  for (int i = head[x]; i; i = e[i].a) {
    if (e[i].b == y) continue;
    ++cnt;
    f[e[i].b][0] = f[e[i].b][1] = e[i].c;
    dfs(e[i].b, x);
    q1.push(sb1(f[e[i].b][1], e[i].b));
  }
  if (cnt + 1 <= k) {
    while (!q1.empty()) f[x][1] += q1.top().x, q1.pop();
    for (int i = head[x]; i; i = e[i].a) {
      if (e[i].b == y) continue;
      f[x][0] = max(f[x][0], f[x][1] - f[e[i].b][1] + f[e[i].b][0]);
    }
    return;
  }
  int rest = k - 1;
  for (int i = 1; i <= rest; i++) {
    int a = q1.top().id;
    q1.pop();
    f[x][1] += f[a][1];
  }
  int sz = 0;
  for (int i = head[x]; i; i = e[i].a) {
    if (e[i].b == y) continue;
    if (sz <= rest)
      q2.push(sb2(f[e[i].b][1], e[i].b)), ++sz;
    else if (f[e[i].b][1] > q2.top().x)
      q2.pop(), q2.push(sb2(f[e[i].b][1], e[i].b));
  }
  int mi = q2.top().x, ma = 0;
  q2.pop();
  while (!q2.empty()) ma += q2.top().x, vis[q2.top().id] = 1, q2.pop();
  for (int i = head[x]; i; i = e[i].a) {
    if (e[i].b == y) continue;
    if (!vis[e[i].b])
      f[x][0] = max(f[x][0], val + f[e[i].b][0] + ma);
    else
      f[x][0] = max(f[x][0], val + f[e[i].b][0] + ma - f[e[i].b][1] + mi);
  }
}
signed main() {
  read(n), read(k);
  for (int i = 1, x, y, z; i < n; i++)
    read(x), read(y), read(z), ++x, ++y, add(x, y, z), add(y, x, z);
  if (k == 1) {
    dfs1(1, 0);
    printf("%d", f[1][0]);
    return 0;
  }
  dfs(1, 0);
  printf("%d", f[1][0]);
  return 0;
}
