You are given a tree (an undirected connected acyclic graph) consisting of n vertices and n - 1 edges. A number is written on each edge, each number is either 0 (let's call such edges 0-edges) or 1 (those are 1-edges).

Let's call an ordered pair of vertices (x, y) (x ≠ y) valid if, while traversing the simple path from x to y, we never go through a 0-edge after going through a 1-edge. Your task is to calculate the number of valid pairs in the tree.

Input

The first line contains one integer n (2 ≤ n ≤ 200000) — the number of vertices in the tree.

Then n - 1 lines follow, each denoting an edge of the tree. Each edge is represented by three integers x_i, y_i and c_i (1 ≤ x_i, y_i ≤ n, 0 ≤ c_i ≤ 1, x_i ≠ y_i) — the vertices connected by this edge and the number written on it, respectively.

It is guaranteed that the given edges form a tree.

Output

Print one integer — the number of valid pairs of vertices.

Example

Input


7
2 1 1
3 2 0
4 2 1
5 2 0
6 7 1
7 2 1


Output


34

Note

The picture corresponding to the first example:

<image>
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Zimba
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		D01Tree solver = new D01Tree();
		solver.solve(1, in, out);
		out.close();
	}

	static class D01Tree {
		int n;
		PrintWriter out;
		InputReader in;
		long ans = 0;
		DisjointUnionSets[] dsu;

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int t, i, j, tt, k;
			this.out = out;
			this.in = in;
			n = in.nextInt();
			dsu = new DisjointUnionSets[2];
			dsu[0] = new DisjointUnionSets(n + 1);
			dsu[1] = new DisjointUnionSets(n + 1);
			for (i = 0; i < n - 1; i++) {
				int x = in.nextInt();
				int y = in.nextInt();
				int c = in.nextInt();
				dsu[c].union(x, y);
			}
			for (i = 1; i <= n; i++) {
				ans += (long)dsu[0].size[dsu[0].find(i)] * dsu[1].size[dsu[1].find(i)] - 1;
			}
			out.println(ans);
		}

		class DisjointUnionSets {
			int[] size;
			int[] parent;
			int n;

			public DisjointUnionSets(int n) {
				size = new int[n];
				parent = new int[n];
				this.n = n;
				makeSet();
			}

			void makeSet() {
				for (int i = 0; i < n; i++) {
					parent[i] = i;
					size[i] = 1;
				}
			}

			int find(int x) {
				if (parent[x] != x) {
					parent[x] = find(parent[x]);
				}
				return parent[x];
			}

			void union(int x, int y) {
				int xRoot = find(x), yRoot = find(y);
				if (xRoot == yRoot) {
					return;
				}
				if (size[xRoot] < size[yRoot]) {
					parent[xRoot] = yRoot;
					size[yRoot] += size[xRoot];
				} else if (size[yRoot] < size[xRoot]) {
					parent[yRoot] = xRoot;
					size[xRoot] += size[yRoot];
				} else {
					parent[yRoot] = xRoot;
					size[xRoot] += size[yRoot];
				}
			}

		}

	}

	static class InputReader {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (numChars == -1) {
				throw new UnknownError();
			}
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new UnknownError();
				}
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public String next() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuffer res = new StringBuffer();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));

			return res.toString();
		}

		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

	}
}

