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.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;


public class Main {

	static class Task {
		
		int NN = 200005;
		int MOD = 1000000007;
		int INF = 2000000000;
		long INFINITY = 2000000000000000000L;
		
		List<Edge> [] g;
		int [][] c;
		int [][] dp;
		int [] pa;
		
		class Edge {
			Integer v, c;

			public Edge(Integer v, Integer c) {
				super();
				this.v = v;
				this.c = c;
			}
		}
		
		void dfs(int x, int px) {
			pa[x] = px;
			c[x][0] = c[x][1] = 0;
			for(Edge e: g[x]) {
				if(e.v==px)
					continue;
				dfs(e.v, x);
				if(e.c == 0) {
					c[x][0] += c[e.v][0] + 1;
				} else {
					c[x][1] += c[e.v][1] + 1;
				}
			}
		}
		
		void dfs1(int x, int f) {
			dp[x][0] = c[x][0];
			dp[x][1] = c[x][1];
			if(pa[x] != -1) {
				dp[x][f] += dp[pa[x]][f] - c[x][f];
			}
			for(Edge e: g[x]) {
				if(e.v == pa[x])
					continue;
				dfs1(e.v, e.c);
			}
		}
		
		public void solve(InputReader in, PrintWriter out) {
			int n = in.nextInt();
			c = new int[n+1][2];
			dp = new int[n+1][2];
			pa = new int[n + 1];
			g = new ArrayList[n + 1];
			for(int i=1;i<=n;++i) {
				g[i] = new ArrayList<>();
				c[i][0] = c[i][1] = 0;
				dp[i][0] = dp[i][1] = 0;
			}
			for(int i=1;i<n;++i) {
				int u = in.nextInt();
				int v = in.nextInt();
				int c = in.nextInt();
				g[u].add(new Edge(v, c));
				g[v].add(new Edge(u, c));
			}
			dfs(1, -1);
			dfs1(1, -1);
			long ans = 0;
			for(int i=1;i<=n;++i) {
				ans += 1L * dp[i][0];
				ans += 1L * dp[i][1];
				ans += 1L * dp[i][0] * dp[i][1];
			}
			out.println(ans);
		}
		
	}
	
	static void prepareIO(boolean isFileIO) {
		//long t1 = System.currentTimeMillis();
		Task solver = new Task();
		// Standard IO
		if(!isFileIO) { 
			InputStream inputStream = System.in;
	        OutputStream outputStream = System.out;
	        InputReader in = new InputReader(inputStream);
	        PrintWriter out = new PrintWriter(outputStream);
	        solver.solve(in, out);
	        //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        out.close();
		}
        // File IO
		else {
			String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in";
	        String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out";
	        InputReader fin = new InputReader(IPfilePath);
	        PrintWriter fout = null;
	        try {
				fout = new PrintWriter(new File(OPfilePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	        solver.solve(fin, fout);
	        //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        fout.close();
		}
	}
	
	public static void main(String[] args) {
        prepareIO(false);
	}
	
	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        
        public InputReader(String filePath) {
        	File file = new File(filePath);
            try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            tokenizer = null;
        }
        
        public String nextLine() {
        	String str = "";
        	try {
				str = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return str;
        }
        
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
        	return Double.parseDouble(next());
        }
        
    }

}