N,K = map(int,input().split()) E = [[] for _ in range(N)] AB = [] L = [] for i in range(N-1): a,b,c = map(int,input().split()) a -= 1 b -= 1 E[a].append((b,c,i)) E[b].append((a,c,i)) AB.append((a,b)) C = [[] for _ in range(N-1)] #子孫に葉が幾つあるか def dfs(x,p= -1): f = True ret = 0 for y,c,id in E[x]: if y != p: f = False a = dfs(y,x) C[id] = [a,c,id] ret += a if f: return 1 return ret dfs(0) C.sort(key = lambda x:(-x[0],-x[1])) ans = 0 for a,c,id in C: ans += a * c if K >= c: K -= c ans += a * c print(ans)