from collections import defaultdict, deque n = int(input()) edge = defaultdict(list) for _ in range(n - 1): u, v, w = map(int, input().split()) u -= 1 v -= 1 edge[u].append((v, w)) edge[v].append((u, w)) ans = 0 Parents = [-1 for _ in range(n)] visited = set([0]) Que = deque([0]) Topo = [] while Que: curr = Que.pop() Topo.append(curr) for nex, _ in edge[curr]: if nex in visited: continue visited.add(nex) Parents[nex] = curr Que.append(nex) Size = [1 for _ in range(n)] for curr in Topo[::-1]: for nex, w in edge[curr]: if nex == Parents[curr]: continue ans += Size[nex] * (n - Size[nex]) * w Size[curr] += Size[nex] print(2 * ans)