# nadare様のコード参考 # https://yukicoder.me/submissions/374115 N = int(input()) size = [1] * N depth = [-1] * N G = [[] for i in range(N)] E = [] for i in range(N - 1): a, b, w = map(int, input().split()) a, b = a - 1, b - 1 G[a].append([b, w]) G[b].append([a, w]) E.append([a, b, w]) depth[0] = 0 visited = [False] * N stack = [0] rev_follow = [] while stack: n = stack.pop() visited[n] = True for to, w in G[n]: if visited[to]: continue stack.append(to) depth[to] = depth[n] + 1 rev_follow.append([n, to]) while rev_follow: a, b = rev_follow.pop() size[a] += size[b] ans = 0 for a, b, w in E: if depth[a] < depth[b]: a, b = b, a ans += 2 * w * size[a] * (N - size[a]) print(ans)