import java.util.*; public class Main { static long total = 0; static int n; static ArrayList> graph; public static void main (String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); graph.get(a).put(b, c); graph.get(b).put(a, c); } for (int x : graph.get(0).keySet()) { calc(x, 0); } System.out.println(total); } static int calc(int idx, int parent) { int sum = 1; for (int x : graph.get(idx).keySet()) { if (x == parent) { continue; } sum += calc(x, idx); } total += graph.get(idx).get(parent) * (long)(n - sum) * sum * 2; return sum; } }