import queue N = int(input()) edge = [[] for i in range(N)] size = [1 for i in range(N)] parent = [[-1,-1] for i in range(N)] for i 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]) q = queue.Queue() s = queue.LifoQueue() parent[0] = [0,0] q.put(0) while q.qsize() > 0: f = q.get() for t,w in edge[f]: if parent[t][0] == -1: parent[t] = [f,w] q.put(t) s.put(f) ans = 0 while s.qsize() > 0: f = s.get() size[parent[f][0]] += size[f] ans += size[f]*(N-size[f])*parent[f][1]*2 print(ans)