n = int(input()) uvw = [[] for i in range(n)] ki = [] for i in range(n-1): u,v,w = map(int,input().split()) u -= 1 v -= 1 uvw[u].append([v,w]) uvw[v].append([u,w]) ki.append([u,v,w]) li = [1]*n def dfs(v,pre): global li for V,w in uvw[v]: if V == pre: continue li[v] += dfs(V,v) return li[v] dfs(0,-1) ans = 0 for i in range(n-1): u,v,w = ki[i] val = min(li[u],li[v]) ans += w*val*(n-val) print(ans*2)