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.util.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
        public void dfs(ArrayList<Integer>[] tree,int source,int[] visited,dsu d) {
            visited[source]=1;
            for(int i:tree[source]) {
                if(visited[i]!=1) {
                    d.union(source, i);
                    this.dfs(tree, i, visited, d);
                }
            }
        }
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        //Reader.init(System.in);
       // Scanner sc=new Scanner(System.in);
        FastReader sc=new FastReader(); 
        int n=sc.nextInt();
        ArrayList<Integer>[] onetree=new ArrayList[n];
        ArrayList<Integer>[] zerotree=new ArrayList[n];
        for(int i=0;i<n;i++) {
            onetree[i]=new ArrayList<Integer>();
            zerotree[i]=new ArrayList<Integer>();
        }
        for(int i=0;i<n-1;i++) {
            int a=sc.nextInt()-1;
            int b=sc.nextInt()-1;
            int v=sc.nextInt();
            if(v==0) {
                zerotree[a].add(b);
                zerotree[b].add(a);
            }
            else {
                onetree[a].add(b);
                onetree[b].add(a);
            }
        }
        Main mm=new Main();
        dsu dzero=new dsu(n);
        dsu done=new dsu(n);
        int[] visited=new int[n];
        int[] size=new int[n];
        for(int i=0;i<n;i++) {
            if(visited[i]==0) {
                mm.dfs(zerotree, i, visited, dzero);
            }
        }
        Arrays.fill(visited, 0);
        for(int i=0;i<n;i++) {
            if(visited[i]==0) {
                mm.dfs(onetree,i, visited, done);
            }
        }
        long[] freq0=new long[n];
        long[] freq1=new long[n];
        for(int i=0;i<n;i++) {
            freq0[dzero.parent[i]]++;
            freq1[done.parent[i]]++;
        }
//      for(int i=0;i<n;i++) {
//          System.out.print(freq0[i]+" ");
//      }
//      System.out.println();
//      for(int i=0;i<n;i++) {
//          System.out.print(freq1[i]+" ");
//      }
//      System.out.println();
        long ans=0;
        for(int i=0;i<n;i++) {
            if(zerotree[i].size()>0 && onetree[i].size()>0) {
                ans+=(freq0[dzero.find(i)]-1)*(freq1[done.find(i)]-1);
            }
        }
        //System.out.println(ans);
        for(int i=0;i<n;i++) {
            ans+=freq0[i]*(freq0[i]-1);
            ans+=freq1[i]*(freq1[i]-1);
        }
        System.out.println(ans);
    }
}  
class dsu{
int[] parent;
int[] rank;
dsu(int a){
    parent=new int[a];
    rank=new int[a];
    for(int i=0;i<a;i++) {
        parent[i]=i;
        rank[i]=0;
    }
}
public int find(int a) {
    int temp=a+0;
    while(parent[temp]!=temp) {
        temp=parent[temp];
    }
    parent[a]=temp;
    return temp;
}
public boolean istrue(int a,int b){
    if(find(a)!=find(b)) {
        return false;
    }

else {
    return true;
}
}
public void union(int a,int b) {
    int x=find(a);
    int y=find(b);
    if(x!=y) {
        if(rank[x]==rank[y]) {
            parent[x]=y;
            rank[y]+=1;
        }
        else if(rank[x]>rank[y]) {
            parent[y]=x;
        }
        else {
            parent[x]=y;
        }
    }
}
}
class pair{
    int f;int s;
    pair(int f,int s){
        this.f=f;
        this.s=s;
    }
}
class a implements Comparator<pair>{
    public int compare(pair p1,pair p2) {
        return p1.f-p2.f;
    }
}
class FastReader 
{ 
    BufferedReader br; 
    StringTokenizer st; 

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

    String next() 
    { 
        while (st == null || !st.hasMoreElements()) 
        { 
            try
            { 
                st = new StringTokenizer(br.readLine()); 
            } 
            catch (IOException  e) 
            { 
                e.printStackTrace(); 
            } 
        } 
        return st.nextToken(); 
    } 

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

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

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

    String nextLine() 
    { 
        String str = ""; 
        try
        { 
            str = br.readLine(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
} 
