from collections import deque N = int(input()) E = [[] for _ in range(N)] m = 10**3 for _ in range(N-1): u,v,w = map(int, input().split()) E[u-1].append((v-1)*m+w) E[v-1].append((u-1)*m+w) q = deque() q.append(0) used = [0]*N used[0] = 1 L = [0] while q: v = q.pop() for e in E[v]: if used[e//m]: continue used[e//m] = 1 q.append(e//m) L.append(e//m) ans = 0 num = [1]*N used = [0]*N L.reverse() for v in L: used[v] = 1 for e in E[v]: if used[e//m]: continue ans += num[v]*(N-num[v])*(e%m) num[e//m] += num[v] print(2*ans)