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.*;
import java.util.*;
 
/*
Solution Sketch:
*/

public class tree {

    static final boolean stdin = true;
    static final String filename = "";
    static FastScanner br;
    static PrintWriter pw;

    public static void main(String[] args) throws IOException {

        if (stdin) {
            br = new FastScanner();
            pw = new PrintWriter(new OutputStreamWriter(System.out));
        } else {
            br = new FastScanner(filename + ".in");
            pw = new PrintWriter(new FileWriter(filename + ".out"));
        }

        X solver = new X();
        solver.solve(br, pw);
    }

    static class X {
        public void solve(FastScanner br, PrintWriter pw) throws IOException {
            int N = br.nextInt();
            DSU zero = new DSU(N);
            DSU one = new DSU(N);
            for (int i = 0; i < N - 1; i++) {
                int x = br.nextInt(); int y = br.nextInt(); int c = br.nextInt();
                x--; y--;
                if (c == 0) {
                    zero.union(x, y);
                }
                else {
                    one.union(x, y);
                }
//                for(int j = 0; j < N; j++){
//                    pw.println(j + " " + zero.find(j) + " " + one.find(j));
//                }
//                pw.println();
            }

            long ans = 0;
            boolean[] vis0 = new boolean[N];
            boolean[] vis1 = new boolean[N];
            for(int i = 0; i < N; i++){ //deal with CC's of size 0 and 1
                if(!vis0[zero.find(i)]){
                    int p = zero.find(i);
                    if(zero.sz(p) > 1) {
//                        pw.println("zero: " + p + " " + zero.sz(p));
                        ans += ((long)zero.sz(p) * ((long)(zero.sz(p)-1)));
                    }
                    vis0[p] = true;
                }
                if(!vis1[one.find(i)]){
                    int p = one.find(i);
                    if(one.sz(p) > 1) {
//                        pw.println("one: " + p + " " + one.sz(p));
                        ans += ((long)one.sz(p) * (long)(one.sz(p)-1));
                    }
                    vis1[p] = true;
                }
                }
//            pw.println(ans);

            //deal with 0-1 case, for all CC of 1 multiply by number of other CC's (start at N then every merge --
            for(int i = 0; i < N; i++){
                int p = one.find(i); int q = zero.find(i);
                    ans += ((long)(one.sz(p)-1) * (long)(zero.sz(q)-1));
            }
            pw.println(ans);
            pw.close();
        }
        class DSU{
             int N;
             int[] par, size;
             DSU(int N){
                this.N = N;
                par = new int[N]; size = new int[N];
                for(int i = 0; i < N; i++) {
                    par[i] = i;
                    size[i] = 1;
                }
            }
             int find(int x){ //wrong?
                if(x == par[x]){
                    return x;
                }
                return par[x] = find(par[x]);
            }
              void union(int x, int y){
                int p = find(x); int q = find(y);
                if(p != q){
                    if(size[p] < size[q]){
                        int temp = p;
                        p = q;
                        q = temp;
                    }
                    par[q] = p;
                    size[p] += size[q];

                }
            }
              int sz(int x){ return size[x];}
        }
    }

    //fastscanner class
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String nextToken() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(nextToken());
        }

        long nextLong() {
            return Long.parseLong(nextToken());
        }

        double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }

}

