import sys sys.setrecursionlimit(10 ** 7) N = int(input()) G = [dict() for _ in range(N)] for _ in range(N - 1): u, v, w = map(int, input().split()) u -= 1 v -= 1 G[u][v] = w G[v][u] = w p = -1 ans = 0 def dfs(v, pre=-1): global p, ans p += 1 v_in = p for nx in G[v].keys(): if nx == pre: continue dfs(nx, v) p += 1 v_out = p if pre != -1: ans += (size := (v_out - v_in) // 2 + 1) * (N - size) * 2 * G[v][pre] dfs(0) print(ans)