#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
string to_string(string s) { return '"' + s + '"'; }
string to_string(char s) { return string(1, s); }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A>
string to_string(A);
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool f = 1;
  string r = "{";
  for (const auto &x : v) {
    if (!f) r += ", ";
    f = 0;
    r += to_string(x);
  }
  return r + "}";
}
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
const int N = 1e5 + 10;
int n, k;
vector<pair<int, int>> g[N];
vector<pair<long long, long long>> A1[N];
pair<long long, long long> f(int u, int p) {
  auto &a1 = A1[u];
  for (auto it : g[u])
    if (it.first != p) {
      int v = it.first, c = it.second;
      auto now = f(v, u);
      a1.emplace_back(now.first + c, max(now.first, now.second) + c);
    }
  sort((a1).begin(), (a1).end());
  reverse((a1).begin(), (a1).end());
  pair<long long, long long> ans(0, 0);
  for (auto i = (0); i <= (min(int(a1.size()), k - 1) - 1); ++i) {
    ans.first += a1[i].first;
    ans.second += a1[i].first;
  }
  long long cur = ans.second;
  for (auto i = (0); i <= (int(a1.size()) - 1); ++i) {
    if (i <= k - 2) {
      ans.second =
          max(ans.second, cur - a1[i].first + a1[i].second +
                              (k - 1 < int(a1.size()) ? a1[k - 1].first : 0));
    } else {
      ans.second = max(ans.second, cur + a1[i].second);
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (auto i = (1); i <= (n - 1); ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    g[x].emplace_back(y, c);
    g[y].emplace_back(x, c);
  }
  pair<int, int> ans = f(0, -1);
  cout << max(ans.first, ans.second) << "\n";
  return 0;
}
