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>
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;








// Please name your class Main
public class Main {
    static FastScanner fs=new FastScanner();
    static class FastScanner {//scanner from SecondThread
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        public String next() {
            while (!st.hasMoreElements())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        int Int() {
            return Integer.parseInt(next());
        }

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

        String Str(){
            return next();
        }
    }


    public static void main (String[] args) throws java.lang.Exception {
        PrintWriter out = new PrintWriter(System.out);
        int T=1;
        for(int t=0;t<T;t++){
            int n = Int();
            int A[][]=new int[n-1][3];
            for(int i=0;i<n-1;i++){
                A[i][0]=Int()-1;
                A[i][1]=Int()-1;
                A[i][2]=Int();
            }


            Solution sol=new Solution();
            sol.solution(out,A,n);
        }
        out.flush();

    }

    public static int Int(){
        return fs.Int();
    }
    public static long Long(){
        return fs.Long();
    }
    public static String Str(){
        return fs.Str();
    }

}



class Solution{

    public void solution(PrintWriter out,int A[][],int n){
        long res=0;
        int nums1[]=new int[n];
        int nums2[]=new int[n];

        for(int i=0;i<n;i++){
            nums1[i]=i;
            nums2[i]=i;
        }

        for(int i=0;i<A.length;i++){
            int u=A[i][0],v=A[i][1],w=A[i][2];

            if(w==1){
                int r1=find(nums1,u);
                int r2=find(nums1,v);
                if(r1!=r2)nums1[r1]=r2;
            }
            else{
                int r1=find(nums2,u);
                int r2=find(nums2,v);
                if(r1!=r2)nums2[r1]=r2;
            }
        }
        Map<Integer,Integer>f1=new HashMap<>();
        Map<Integer,Integer>f2=new HashMap<>();

        res=res+cal(nums1,f1)+cal(nums2,f2);

       

        for(int i=0;i<n;i++){
            int r1=find(nums1,i);
            int r2=find(nums2,i);


            long cnt1=f1.get(r1);
            long cnt2=f2.get(r2);
            res=res+(cnt1-1)*(cnt2-1);
        }


        out.println(res);

    }

    public long cal(int nums[],Map<Integer,Integer>f){
        long res=0;
        for(int i=0;i<nums.length;i++){
            int r=find(nums,i);
            if(!f.containsKey(r)){
                f.put(nums[i],1);
            }
            else{
                f.put(nums[i],f.get(nums[i])+1);
            }
        }

        for(Integer key:f.keySet()){
            long val=f.get(key);
            res=res+(val)*(val-1);
        }
        return res;
    }


    public int find(int nums[],int x){//union find => find method
        if(nums[x]==x)return x;
        int root=find(nums,nums[x]);
        nums[x]=root;
        return root;
    }

}

