# 部分木、子の数、DFS # dfs2回は必要なかったので効率的な方法でやり直す 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] root = 1 dfs(root) #print(child) ans = 0 for u, v, w in edge_list: mn = min(child[u], child[v]) ans += mn*(N-mn)*w*2 print(ans)