import sys input = sys.stdin.buffer.readline from collections import deque import heapq 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) s = 10**6 q = deque() q.append(0) rnk = [-1]*N rnk[0] = 0 while q: temp = q.popleft() v = temp//s r = temp%s for e in E[v]: if rnk[e//m] >= 0: continue rnk[e//m] = r+1 q.append(e//m*s+r+1) h = [] num = [1]*N for i in range(N): if len(E[i]) == 1: heapq.heappush(h, -rnk[i]*s+i) used = [0]*N used[0] = 1 while h: temp = heapq.heappop(h) v = temp%s used[v] = 1 for e in E[v]: if used[e//m] == 0: num[e//m] += num[v] heapq.heappush(h, -rnk[e//m]*s+e//m) ans = 0 for i in range(N): for e in E[i]: n = min(num[i], num[e//m]) ans += n*(N-n)*(e%m) print(ans)