N, K = map(int,input().split()) G = [[] for k in range(N)] for k in range(N-1): a, b, c = map(int,input().split()) G[a-1].append([b-1,c]) G[b-1].append([a-1,c]) visited = [-1]*N visited[0] = 1 ha = dict() def dfs(ima,mae,keiro): if len(G[ima]) == 1: for e in keiro: if e in ha: ha[e] += 1 else: ha[e] = 1 for tsugi in G[ima]: if tsugi != mae: if visited[tsugi[0]] == -1: visited[tsugi[0]] = 1 dfs(tsugi[0],ima,keiro+[(ima,tsugi[0])]) dfs(0,-1,[]) ans = 0 nodes = [] for k in range(N): for e in G[k]: if (k,e[0]) in ha: ans += e[1]*ha[(k,e[0])] nodes.append([e[1],e[1]*ha[(k,e[0])]]) N = len(nodes) dp = [[0]*(K+1) for k in range(N+1)] for i in range(N): for w in range(K+1): if w >= nodes[i][0]: dp[i+1][w] = max(dp[i][w],dp[i][w-nodes[i][0]]+nodes[i][1]) else: dp[i+1][w] = dp[i][w] print(ans+dp[N][K])