import sys import pypyjit import itertools import heapq import math from collections import deque, defaultdict import bisect input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) pypyjit.set_param('max_unroll_recursion=-1') N = int(input()) adj = [{} for _ in range(N)] for _ in range(N - 1): u, v, w = map(int, input().split()) u -= 1 v -= 1 adj[u][v] = w adj[v][u] = w child = [None] * N cost = [None] * N cost[0] = 0 def dfs(v, p): if v > 0: cost[v] = cost[p] + adj[v][p] acc = 1 for d in adj[v].keys(): if d == p: continue acc += dfs(d, v) child[v] = acc return child[v] dfs(0, -1) S = sum(cost) ans = 0 def dfs2(v, p): global ans, S if v > 0: S += adj[v][p] * (N - 2 * child[v]) ans += S for d in adj[v].keys(): if d == p: continue dfs2(d, v) if v > 0: S -= adj[v][p] * (N - 2 * child[v]) dfs2(0, -1) print(ans)