#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 123;
const long long LINF = (long long)1e18 + 123;
const long double EPS = (long double)1e-7;
const long long MOD = (long long)1e9 + 7;
long long power(long long x, long long y, long long mod = MOD) {
  if (y == 0) {
    return 1;
  }
  if (y & 1) {
    return power(x, y - 1, mod) * x % mod;
  } else {
    long long tmp = power(x, y / 2, mod);
    return tmp * tmp % mod;
  }
}
void add(long long &x, long long y, long long mod = MOD) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
}
long long sum(long long x, long long y, long long mod = MOD) {
  add(x, y, mod);
  return x;
}
long long mult(long long x, long long y, long long mod = MOD) {
  return (x * y) % mod;
}
long long div(long long x, long long y, long long mod) {
  return x * power(y, mod - 2, mod);
}
template <typename A, typename B>
bool mini(A &x, const B &y) {
  if (y < x) {
    x = y;
    return true;
  }
  return false;
}
template <typename A, typename B>
bool maxi(A &x, const B &y) {
  if (y > x) {
    x = y;
    return true;
  }
  return false;
}
template <typename T>
void read(T first, T last) {
  while (first != last) {
    cin >> *(first++);
  }
}
template <typename T>
void print(T first, T last) {
  while (first != last) {
    cout << *(first++) << " ";
  }
  cout << "\n";
}
void run();
int main() {
  if (strlen("")) {
    freopen(
        ""
        ".in",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  run();
  return 0;
}
const int N = (int)1e5 + 123;
int n, k;
vector<pair<int, int>> g[N];
long long dp1[N], dp2[N];
void dfs(int v, int from = -1) {
  vector<pair<pair<long long, long long>, int>> a;
  int cc = 0;
  for (auto &e : g[v]) {
    int u = e.first;
    if (u == from) continue;
    dfs(u, v);
    a.push_back({{dp1[u] + e.second, dp2[u] + e.second}, cc});
    cc++;
  }
  sort((a).begin(), (a).end());
  reverse((a).begin(), (a).end());
  int t = min(k - 1, (int)(a).size());
  for (int i = 0; i < t; i++) dp1[v] += a[i].first.first;
  for (int i = 0; i < (int)(a).size(); i++) {
    if (i < t) {
      maxi(dp2[v], a[i].first.second + dp1[v] - a[i].first.first +
                       (t >= (int)(a).size() ? 0 : a[t].first.first));
    } else {
      maxi(dp2[v], a[i].first.second + dp1[v]);
    }
  }
}
void run() {
  cin >> n >> k;
  for (int i = 0; i < n - 1; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
  }
  dfs(0);
  cout << dp2[0] << "\n";
}
