import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,K = MI()
Graph = [[] for _ in range(N+1)]
for _ in range(N-1):
    a,b,c = MI()
    Graph[a].append((b,c))
    Graph[b].append((a,c))

X = []
ans = 0


@lru_cache(maxsize=None)
def f(i,par):
    global ans
    count = 0
    for j,c in Graph[i]:
        if j == par:
            continue
        d = f(j,i)
        count += d
        X.append((c,c*d))
        ans += c*d
    if count == 0:
        return 1
    return count


f(1,0)

dp = [[-1]*(K+1) for _ in range(N)]
dp[0][0] = 0
for i in range(1,N):
    w,v = X[i-1]
    for j in range(K+1):
        if dp[i-1][j] >= 0:
            dp[i][j] = dp[i-1][j]
        if j >= w and dp[i-1][j-w] >= 0:
            dp[i][j] = max(dp[i][j],dp[i-1][j-w]+v)

M = max(dp[-1])
if M > 0:
    ans += M

print(ans)