#include <bits/stdc++.h>
const int N = 100010;
std::vector<std::pair<int, int> > e[N];
int dp[N][2];
int n, k;
void dfs(int u, int fa) {
  std::vector<int> vec;
  int sum = 0;
  for (auto v : e[u]) {
    if (v.first == fa) {
      continue;
    }
    dfs(v.first, u);
    vec.push_back(dp[v.first][1] + v.second);
  }
  std::sort(vec.begin(), vec.end(), std::greater<int>());
  int sz = std::min(k - 1, (int)vec.size());
  for (int i = 0; i < sz; ++i) {
    dp[u][1] += vec[i];
  }
  for (auto v : e[u]) {
    if (v.first == fa) {
      continue;
    }
    int x = v.second + dp[v.first][0], y = v.second + dp[v.first][1];
    if (y < vec[sz - 1] || !sz) {
      dp[u][0] = std::max(dp[u][0], x + dp[u][1]);
    } else {
      if (k - 1 < vec.size()) {
        dp[u][0] = std::max(dp[u][0], x + dp[u][1] - y + vec[k - 1]);
      } else {
        dp[u][0] = std::max(dp[u][0], x + dp[u][1] - y);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0, u, v, w; i < n - 1; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    e[u].push_back({v, w});
    e[v].push_back({u, w});
  }
  dfs(0, -1);
  return printf("%d\n", std::max(dp[0][0], dp[0][1])), 0;
}
