def main(): import sys from collections import deque input = sys.stdin.readline N = int(input()) adj = [[] for _ in range(N+1)] E = {} for _ in range(N-1): a, b, w = map(int, input().split()) adj[a].append(b) adj[b].append(a) E[a*(N+1)+b] = w E[b*(N+1)+a] = w que = deque() que.append(1) seen = [-1] * (N+1) seen[1] = 0 par = [0] * (N+1) child = [[] for _ in range(N+1)] seq = [] while que: v = que.popleft() seq.append(v) for u in adj[v]: if seen[u] == -1: seen[u] = seen[v] + 1 par[u] = v child[v].append(u) que.append(u) seq.reverse() dp = [1] * (N+1) ans = 0 for v in seq: for u in child[v]: dp[v] += dp[u] ans += E[v*(N+1)+u] * dp[u] * (N-dp[u]) * 2 print(ans) if __name__ == '__main__': main()