N = int(input()) edge_list = [] edge_dic = {} edges = [[] for i in range(N+1)] for i in range(N-1): u, v, w = map(int, input().split()) edges[u].append(v) edges[v].append(u) edge_list.append((u, v, w)) edge_dic[(u, v)] = w edge_dic[(v, u)] = w child = [0]*(N+1) visited = [0]*(N+1) import sys sys.setrecursionlimit(10**7) def dfs(current): visited[current] = 1 child[current] += 1 for nxt in edges[current]: if visited[nxt] == 0: dfs(nxt) child[current] += child[nxt] for i in range(1, N+1): if len(edges[i]) == 1: root = i break dfs(root) #print(child) def dfs2(current, prev): global ans for nxt in edges[current]: if nxt != prev: #print('current', current, 'nxt', nxt, child[nxt], (N-child[nxt]), edge_dic[(current, nxt)]) ans += child[nxt]*(N-child[nxt])*edge_dic[(current, nxt)] dfs2(nxt, current) ans = 0 dfs2(root, 0) print(ans*2)