import sys input = sys.stdin.readline N,K=map(int,input().split()) E=[tuple(map(int,input().split())) for i in range(N-1)] EDGE=[[] for i in range(N)] for a,b,e in E: a-=1 b-=1 EDGE[a].append((b,e)) EDGE[b].append((a,e)) QUE=[0] Parent=[-1]*(N+1) Parent[0]=0 TOP_SORT=[] while QUE: x=QUE.pop() TOP_SORT.append(x) for to,w in EDGE[x]: if Parent[to]==-1: Parent[to]=x,w QUE.append(to) EW=[0]*N for n in TOP_SORT[1:][::-1]: if EW[n]==0: EW[n]=1 EW[Parent[n][0]]+=EW[n] XX=[] for i in range(1,N): XX.append((Parent[i][1],EW[i])) XC=[[] for i in range(101)] for x,c in XX: XC[c].append(x) ANS=0 cost=0 for i in range(100,-1,-1): if sum(XC[i])+cost<=K: ANS+=sum(XC[i])*i*2 cost+=sum(XC[i]) else: rest=K-cost DP={0} for j in XC[i]: NDP=DP.copy() for d in DP: if d+j<=rest: NDP.add(d+j) DP=NDP MAX=max(DP) SUM=sum(XC[i]) ANS+=((SUM-MAX)+MAX*2)*i cost+=MAX print(ANS)