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.io.*;

public class d {
    public static ArrayList<Integer>[][] adj;
    public static int n;
    public static TreeMap<Long, Long> memo;
    public static int[] p;
    public static long[][] rootMemo;
    public static void main(String[] Args) throws Exception {
        FS sc = new FS(System.in);
        n = sc.nextInt();
        int i;
        memo = new TreeMap<Long, Long>();
        adj = new ArrayList[2][n];
        for (i = 0; i < n; i++) {
            adj[0][i] = new ArrayList<Integer>();
            adj[1][i] = new ArrayList<Integer>();
        }
        for (i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            adj[c][a].add(b);
            adj[c][b].add(a);
        }
        long ans = 0;
        p = new int[n];
        rootMemo = new long[2][n];
        Arrays.fill(rootMemo[0], -1);
        Arrays.fill(rootMemo[1], -1);
        Arrays.fill(p, -2);
        rootMemo[0][0] = dp(0, -1, 0);
        rootMemo[1][0] = dp(0, -1, 1);
        dp2(0, -1, 0);
        dp2(0, -1, 1);
        for (i = 0; i < n; i++) {
            ans += rootMemo[0][i];
        }
        System.out.println(ans - n);
    }
    
    public static void dp2(int cur, int par, int type) {
        int j;
        if (par != -1 && rootMemo[type][cur] == -1) {
            if (type == p[cur]){
                rootMemo[type][cur] = rootMemo[type][par];
            } else if (type == 1) {
                rootMemo[type][cur] = dp(cur, par, type);
            } else {
                rootMemo[type][cur] = rootMemo[1][par] + 
                    dp(cur, par, 0) -
                    dp(cur, par, 1);
            }
        }
        if (type == 0) {
            for (j = 0; j < 2; j++)
                for (Integer x : adj[j][cur])
                    if (x != par){
                        dp2(x, cur, 1);
                        dp2(x, cur, 0);
                    }
        }
    }
    
    public static long dp(int cur, int par, int type) {
        if(p[cur] == -2)
            p[cur] = type;
        if (type == 1)
            dp(cur, par, 0);
        long key = cur;
        key *= n + 1;
        key += par;
        key *= 3;
        key += type;
        if (memo.containsKey(key))
            return memo.get(key);
        long ans = 1;
        int j;
        for (j = type; j < 2; j++) 
            for (Integer x : adj[j][cur])
                if (x != par)
                    ans += dp(x, cur, j);
        memo.put(key, ans);
        return ans;
    }
    
    public static class FS {
        BufferedReader br;
        StringTokenizer st;
        FS(InputStream in) {
            br = new BufferedReader(new InputStreamReader(System.in));
            st = new StringTokenizer("");
        }
        
        String next() throws Exception {
            if (st.hasMoreTokens())
                return st.nextToken();
            st = new StringTokenizer(br.readLine());
            return next();
        }
        
        int nextInt() throws Exception {
            return Integer.parseInt(next());
        }
    }
}   