import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static Hen[] arr;
	static List<List<Hen>> list;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		arr = new Hen[n - 1];
		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			String[] sa = br.readLine().split(" ");
			Hen h = new Hen();
			h.i = i;
			h.u = Integer.parseInt(sa[0]) - 1;
			h.v = Integer.parseInt(sa[1]) - 1;
			h.w = Integer.parseInt(sa[2]);
			arr[i] = h;
			list.get(h.u).add(h);
			list.get(h.v).add(h);
		}
		br.close();

		dfs(0, -1);

		long ans = 0;
		for (int i = 0; i < arr.length; i++) {
			ans += (long) arr[i].w * arr[i].c * (n - arr[i].c) * 2;
		}
		System.out.println(ans);
	}

	static class Hen {
		int i, u, v, w, c;
	}

	static int dfs(int x, int p) {
		List<Hen> nexts = list.get(x);
		int sum = 1;
		for (Hen h : nexts) {
			int next = h.u;
			if (next == x) {
				next = h.v;
			}
			if (next != p) {
				int c = dfs(next, x);
				h.c = c;
				sum += c;
			}
		}
		return sum;
	}
}