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.
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.util.Collections;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author niquefa_diego
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskK solver = new TaskK();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskK {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            Vertex.K = in.nextInt();
            Vertex[] vs = new Vertex[n];
            for (int i = 0; i < n; ++i)
                vs[i] = new Vertex();
            for (int edges = n - 1; edges-- > 0; ) {
                Vertex a = vs[in.nextInt()];
                Vertex b = vs[in.nextInt()];
                int c = in.nextInt();
                a.addEdge(b, c);
                b.addEdge(a, c);
            }

            vs[0].root(null);
            out.println(vs[0].solve(0));
        }

    }

    static class Edge implements Comparable<Edge> {
        Vertex to;
        int cost;


        public int compareTo(Edge other) {
            return -Long.compare(to.solve(1) + cost, other.to.solve(1) + other.cost);
        }

        Edge(Vertex to, int cost) {
            this.to = to;
            this.cost = cost;
        }

    }

    static class Vertex {
        static int K;
        List<Edge> edges = new ArrayList<>();
        long[] memo = new long[]{-1, -1};

        void addEdge(Vertex to, int cost) {
            edges.add(new Edge(to, cost));
        }

        void root(Vertex parent) {
            int oldSize = edges.size();
            for (Iterator<Edge> it = edges.iterator(); it.hasNext(); )
                if (it.next().to == parent) {
                    it.remove();
                    break;
                }
            for (Edge e : edges)
                e.to.root(this);
            if (edges.size() != oldSize - (parent == null ? 0 : 1))
                throw new RuntimeException();
        }

        long solve(int goback) {
            if (memo[goback] != -1)
                return memo[goback];
            Collections.sort(edges);

            long ansTwoWay = 0;
            for (int i = 0; i < K - 1 && i < edges.size(); ++i)
                ansTwoWay += edges.get(i).to.solve(1) + edges.get(i).cost;

            long ansOneWay = ansTwoWay;
            // one way trip to i-th son, two way trips to other k-1 children
            for (int i = 0; i < edges.size(); ++i) {
                long aux = ansTwoWay;
                if (i < K - 1) {
                    aux -= edges.get(i).to.solve(1);
                    aux += edges.get(i).to.solve(0);
                    if (edges.size() >= K)
                        aux += edges.get(K - 1).to.solve(1) + edges.get(K - 1).cost;
                } else
                    aux += edges.get(i).to.solve(0) + edges.get(i).cost;
                ansOneWay = Math.max(ansOneWay, aux);
            }

            memo[0] = ansOneWay;
            memo[1] = ansTwoWay;
            return memo[goback];
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer = null;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

