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>
//created by Whiplash99
import java.io.*;
import java.util.*;
public class D
{
    static class DSU
    {
        int parent[], size[];
        DSU(int N)
        {
            parent=new int[N];
            size=new int[N];
            for(int i=0;i<N;i++) make_set(i);
        }
        void make_set(int v)
        {
            parent[v]=v;
            size[v]=1;
        }
        int find_set(int v)
        {
            if(v==parent[v])
                return v;
            return parent[v]=find_set(parent[v]);
        }
        boolean union_sets(int a, int b)
        {
            a=find_set(a);
            b=find_set(b);

            if(a==b) return false;

            if(size[a]<size[b])
            {
                int tmp=a;
                a=b;
                b=tmp;
            }
            parent[b]=a;
            size[a]+=size[b];

            return true;
        }
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int i,N;

        N=Integer.parseInt(br.readLine().trim());

        DSU zero=new DSU(N), one=new DSU(N);
        for(i=0;i<N-1;i++)
        {
            String s[]=br.readLine().trim().split(" ");
            int u=Integer.parseInt(s[0])-1;
            int v=Integer.parseInt(s[1])-1;
            int w=Integer.parseInt(s[2]);

            if(w==0) zero.union_sets(u,v);
            else one.union_sets(u,v);
        }

        long ans=0;
        for(i=0;i<N;i++)
        {
            int pZero=zero.find_set(i), pOne=one.find_set(i);

            if(pZero==i) ans+=(long)zero.size[i]*(zero.size[i]-1); //ordered
            if(pOne==i) ans+=(long)one.size[i]*(one.size[i]-1); //ordered

            // Can go through 1 after 0, but not vice versa.
            // So, count only once.
            ans+=(long)(zero.size[pZero]-1)*(one.size[pOne]-1);
        }
        System.out.println(ans);
    }
}