import java.util.*; import java.io.*; public class Main { static long[] chiledren; static long[] costs; static HashMap[] graph; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); graph = new HashMap[n]; for (int i = 0; i < n; i++) { graph[i] = new HashMap(); } for (int i = 0; i < n - 1; i++) { String[] line = br.readLine().split(" ", 3); int a = Integer.parseInt(line[0]) - 1; int b = Integer.parseInt(line[1]) - 1; int c = Integer.parseInt(line[2]); graph[a].put(b, c); graph[b].put(a, c); } chiledren = new long[n]; costs = new long[n]; getChildrenCount(0, -1); long total = 0; for (int i = 0; i < n; i++) { total += chiledren[i] * (n - chiledren[i]) * costs[i]; } System.out.println(total * 2); } static long getChildrenCount(int to, int from) { for (Map.Entry entry : graph[to].entrySet()) { int x = entry.getKey(); if (x == from) { costs[to] += entry.getValue(); } else { chiledren[to] += getChildrenCount(x, to); } } chiledren[to]++; return chiledren[to]; } }