Thank you for helping Heidi! It is now the second of April, but she has been summoned by Jenny again. The pranks do not seem to end...

In the meantime, Heidi has decided that she does not trust her friends anymore. Not too much, anyway. Her relative lack of trust is manifested as follows: whereas previously she would not be made to visit the same person twice, now she can only be sure that she will not be made to visit the same person more than k times. (In the case of Jenny, this includes her first visit in the beginning. The situation from the easy version corresponds to setting k = 1.)

This is not as bad as it looks, since a single ticket for a route between two friends allows Heidi to travel between this pair of friends the whole day (in both directions). In other words, once she pays for travel between a pair of friends, all further travels between that pair are free.

How much money will Heidi waste now, in a worst-case scenario?

Input

The first line contains two space-separated integers – the number of friends n (<image>) and the parameter k (1 ≤ k ≤ 105). The next n - 1 lines each contain three space-separated integers u, v and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104) meaning that u and v are friends and the cost for traveling between u and v is c.

It is again guaranteed that the social network of the input forms a tree.

Output

Again, output a single integer – the maximum sum of costs of tickets.

Examples

Input

9 3
0 1 1
0 2 1
1 3 2
1 4 2
1 5 2
2 6 3
2 7 3
2 8 3


Output

15


Input

9 5
0 1 1
0 2 1
1 3 2
1 4 2
1 5 2
2 6 3
2 7 3
2 8 3


Output

17


Input

11 6
1 0 7932
2 1 1952
3 2 2227
4 0 9112
5 4 6067
6 0 6786
7 6 3883
8 4 7137
9 1 2796
10 5 6200


Output

54092

Note

In the first example, the worst-case scenario for Heidi is to visit the friends in the following order: 0, 1, 5, 1, 3, 1, 0, 2, 6, 2, 7, 2, 8. Observe that no friend is visited more than 3 times.
#include <bits/stdc++.h>
#pragma GCC optimization("O3")
using namespace std;
template <typename T>
inline void Cin(T& first) {
  char c;
  T sign = 1;
  first = 0;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    if (c == '-') sign = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) first = first * 10 + c - '0';
  first *= sign;
}
template <typename T>
inline void Out(T first) {
  if (first > 9) Out(first / 10);
  putchar(first % 10 + '0');
}
template <typename T>
inline void Cout(T first, char c) {
  if (first < 0) putchar('-');
  first = abs(first);
  Out(first);
  putchar(c);
}
template <typename T, typename... Args>
inline void Cin(T& a, Args&... args) {
  Cin(a);
  Cin(args...);
}
template <typename T, typename... Args>
inline void Cout(T a, char c, Args... args) {
  Cout(a, c);
  Cout(args...);
}
const int N = int(1e5) + 7;
vector<pair<int, int> > a[N];
int n, u, v, c, k, k0, k1, f[N][2];
void DFS(int u, int par) {
  vector<pair<int, int> > cur;
  cur.clear();
  for (pair<int, int> p : a[u]) {
    if (p.first == par) continue;
    DFS(p.first, u);
    cur.emplace_back(p.second + f[p.first][0], p.second + f[p.first][1]);
  }
  sort(cur.begin(), cur.end(),
       [](const pair<int, int>& a, const pair<int, int>& b) {
         return a.second > b.second ||
                (a.second == b.second && a.first < b.first);
       });
  int lim = min(k1, int(cur.size())), Max = -1, _Max = -1;
  for (int i = 0; i < lim; ++i) f[u][1] += cur[i].second;
  lim = min(k0, int(cur.size()));
  if (!lim) return;
  for (int i = cur.size() - 1; i + 1 >= lim; --i) Max = max(Max, cur[i].first);
  f[u][0] = Max;
  _Max = Max, Max = int(-1e9);
  for (int i = 0; i < lim - 1; ++i) {
    f[u][0] += cur[i].second;
    Max = max(cur[i].first - cur[i].second, Max);
  }
  f[u][0] = max(f[u][0], f[u][0] - _Max + Max + cur[lim - 1].second);
}
void Enter() {
  cin >> n >> k;
  k1 = k - 1;
  k0 = k;
  for (int i = 1; i < n; ++i) {
    cin >> u >> v >> c;
    a[u].emplace_back(v, c), a[v].emplace_back(u, c);
  }
}
void Solve() {
  DFS(0, 0);
  cout << max(f[0][0], f[0][1]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("test"
            ".inp",
            "r")) {
    freopen(
        "test"
        ".inp",
        "r", stdin);
    freopen(
        "test"
        ".out",
        "w", stdout);
  }
  Enter(), Solve();
  return 0;
}
