import sys sys.setrecursionlimit(400000) n = int(input()) adj = [[] for _ in range(n)] for _ in range(n-1): u, v, w = map(int, input().split()) adj[u-1].append([v-1, w]) adj[v-1].append([u-1, w]) ans = 0 def dfs(x, parent): global ans child_num = 1 for c, w in adj[x]: if c == parent: continue d = dfs(c, x) ans += 2*d*(n-d)*w child_num += d return child_num dfs(0, -1) print(ans)