#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
const int MAXN = (int)1e5 + 9;
const int MOD = (int)(1e9 + 7);
const int LOG2MAXN = 17;
const int INF = 1e9;
const long double eps = 1e-9;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};
char ddir[] = {'R', 'U', 'L', 'D'};
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<pair<int, int> > gg[MAXN];
long long dp[MAXN][2];
int n, k;
char used[MAXN];
bool cmp1(pair<int, int> a, pair<int, int> b) {
  if (dp[a.second][0] > dp[b.second][0]) return 1;
  return 0;
}
void dfs(int v, int p = -1) {
  used[v] = 1;
  dp[v][0] = dp[v][1] = 0;
  for (auto to : gg[v])
    if (!used[to.second]) dfs(to.second, v);
  for (auto &to : gg[v])
    if (to.second != p) {
      dp[to.second][0] += to.first;
      dp[to.second][1] += to.first;
    }
  sort((gg[v]).begin(), (gg[v]).end(), cmp1);
  dp[v][0] = 0;
  int cnt = 0;
  int lst = -1;
  for (int i = 0; i < (int)(gg[v]).size() && cnt < k - 1; i++)
    if (gg[v][i].second != p) {
      dp[v][0] += dp[gg[v][i].second][0];
      cnt++;
    }
  long long plus1 = 0;
  cnt = 0;
  for (int i = 0; i < (int)(gg[v]).size() && cnt < k; i++)
    if (gg[v][i].second != p) {
      plus1 += dp[gg[v][i].second][0];
      cnt++;
      if (cnt == k) lst = i;
    }
  dp[v][1] = plus1;
  long long plus2 = 0;
  if ((int)(gg[v]).size() <= k) lst = (int)(gg[v]).size();
  for (int i = 0; i < (int)(gg[v]).size(); i++)
    if (gg[v][i].second != p) {
      if (i <= lst)
        plus2 = max(plus2,
                    dp[v][1] - dp[gg[v][i].second][0] + dp[gg[v][i].second][1]);
      else
        plus2 = max(plus2, dp[v][0] + dp[gg[v][i].second][1]);
    }
  dp[v][1] = max(dp[v][1], plus2);
}
void solve() {
  dfs(0);
  cout << dp[0][1] << endl;
}
void input() {
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    gg[u].push_back({c, v});
    gg[v].push_back({c, u});
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  input();
  solve();
  return 0;
}
