#include <bits/stdc++.h>
const int maxn = 1e5 + 5;
inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; }
int n, k, head[maxn], tot;
struct Edge {
  int next, to, val;
} e[maxn << 1];
inline void add_edge(int x, int y, int z) {
  e[++tot].next = head[x];
  e[tot].to = y, e[tot].val = z, head[x] = tot;
}
long long dp[maxn][2];
struct shik {
  int id;
  long long val;
  bool operator<(shik b) const { return val > b.val; }
};
std ::vector<shik> son[maxn];
inline bool cmp(int a, int b) { return a > b; }
inline void dfs(int u, int F) {
  for (int i = head[u], v; v = e[i].to, i; i = e[i].next) {
    if (v == F) continue;
    dfs(v, u), son[u].push_back((shik){v, dp[v][0] + e[i].val});
  }
  std ::sort(son[u].begin(), son[u].end());
  for (int i = 0; i < son[u].size(); ++i) {
    dp[u][0] += son[u][i].val;
    if (i >= k - 2) break;
  }
  long long sum = 0;
  for (int i = 0; i < son[u].size(); ++i) {
    sum += son[u][i].val;
    if (i >= k - 1) break;
  }
  for (int i = 0; i < son[u].size(); ++i) {
    dp[u][1] = max(dp[u][1], sum + dp[son[u][i].id][1] - dp[son[u][i].id][0]);
    if (i >= k - 1) break;
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1, x, y, z; i < n; ++i) {
    scanf("%d %d %d", &x, &y, &z), ++x, ++y;
    add_edge(x, y, z), add_edge(y, x, z);
  }
  dfs(1, 0);
  printf("%lld", dp[1][1]);
  return 0;
}
