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.*;
import java.util.*;

public class A implements Runnable {
	public static void main (String[] args) {new Thread(null, new A(), "_cf", 1 << 28).start();}

	int n, k;
	ArrayList<int[]>[] adj;
	long maxAddEnd = 0;
	
	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("");

		n = fs.nextInt();
		k = fs.nextInt();
		adj = new ArrayList[n];
		for(int i = 0; i < n; i++) adj[i] = new ArrayList<>();
		
		for(int i = 0; i < n-1; i++) {
			int u = fs.nextInt(), v = fs.nextInt();
			int we = fs.nextInt();
			adj[u].add(new int[] {v, we});
			adj[v].add(new int[] {u, we});
		}
		
		dp = new long[2][n];
		for(long[] d : dp) Arrays.fill(d, -1);
		
		out.println(solve(0, 1, -1));
		
		out.close();
	}

	long[][] dp;
	long solve(int u, int finHere, int p) {
		if(dp[finHere][u] != -1) return dp[finHere][u];
		
		long res = 0;
		ArrayList<Pair> list = new ArrayList<>();
		
		for(int[] e : adj[u]) {
			int v = e[0], we = e[1];
			if(v == p) continue;
			
			long x1 = solve(v, 0, u) + we;
			
			if(finHere == 1) {
				long x2 = solve(v, 1, u) + we;
				list.add(new Pair(x1, x2));
			}
			else {
				list.add(new Pair(x1, 0));
			}
		}
		
		Collections.sort(list);
		
		if(finHere == 0) {
			for(int i = 0; i < Math.min(k-1, list.size()); i++) {
				res += list.get(i).a;
			}
			return dp[finHere][u] = res;
		}
		else {
			long sum = 0;
			for(int i = 0; i < Math.min(k, list.size()); i++) {
				sum += list.get(i).a;
			}
			res = sum;
			
			long maxim = 0;
			for(int i = k; i < list.size(); i++)
				maxim = Math.max(maxim, list.get(i).b);
			
			for(int i = 0; i < Math.min(k, list.size()); i++) {
				long cur = sum - list.get(i).a;
				cur += Math.max(maxim, list.get(i).b);
				res = Math.max(res, cur);
			}
			return dp[finHere][u] = res;
		}
		
	}

	class Pair implements Comparable<Pair> {
		long a, b;
		Pair(long c, long d) {
			a = c; b = d;
		}
		public int compareTo(Pair p) {
			return Long.compare(p.a, a);
		}
	}
	
	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) {
			try {
				in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
			}
			catch (Exception e) {
				in = new BufferedInputStream(System.in, BS);
			}
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}

	}

}