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>
/**
 * @author derrick20
 */
import java.io.*;
import java.util.*;

public class Tree01 implements Runnable {
    public static void main(String[] args) throws Exception {
        new Thread(null, new Tree01(), "Main", 1 << 29).start();
    }

    public void run() {
        FastScanner sc = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        int N = sc.nextInt();
        adjList = new ArrayList[N];
        Arrays.setAll(adjList, a -> new ArrayList<>());
        for (int i = 0; i < N - 1; i++) {
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            boolean isOne = sc.nextInt() == 1;
            adjList[u].add(new Edge(v, isOne));
            adjList[v].add(new Edge(u, isOne));
        }

        dp0 = new long[N];
        dp1 = new long[N];
        dp01 = new long[N];
        dp10 = new long[N];

        ans = 0;
        solve(0, -1);
//        System.out.println("dp0: " + Arrays.toString(dp0));
//        System.out.println("dp1: " + Arrays.toString(dp1));
//        System.out.println("dp01: " + Arrays.toString(dp01));
//        System.out.println("dp10: " + Arrays.toString(dp10));
        out.println(ans);
        out.close();
    }

    static ArrayList<Edge>[] adjList;
    static long[] dp0, dp1, dp01, dp10;
    static long ans;
/*
dp values store the ways for a given path structure, starting from a
given node, going DOWNWARD!
We can do casework on which node a path goes through: in this case
on the highest node it goes through. In this way, we will count all paths
during each call of the dfs, combining the paths of children to get an
expanded answer.
dp[i]:
a: all 0
b: all 1
c: all 0 then call 1
d: all 1 then all 0

a-a
b-b

a-b

a-c
d-b
 */
    static void solve(int node, int par) {
        long total0 = 0;
        long total1 = 0;
        long total01 = 0;
        long total10 = 0;
        for (Edge e : adjList[node]) {
            int adj = e.v;
            if (adj != par) {
                solve(adj, node);
                if (e.isOne) {
                    dp1[adj] += 1;
                    total1 += dp1[adj];
                    total10 += dp10[adj] + dp0[adj];
                }
                else {
                    dp0[adj] += 1;
                    total0 += dp0[adj];
                    total01 += dp01[adj] + dp1[adj];
                }

                if (e.isOne) {
                    dp10[node] += dp10[adj] + dp0[adj];
                    dp1[node] += dp1[adj];
                }
                else {
                    dp0[node] += dp0[adj];
                    dp01[node] += dp01[adj] + dp1[adj];
                }
            }
        }
//        System.out.println("Going through: " + node);
//        System.out.println(total0 + " " + total1 + " " + total01 + " " + total10);
        for (Edge e : adjList[node]) {
            int adj = e.v;
            if (adj != par) {
                long paired = 0;
                if (e.isOne) {
                    // then we know that we can multiply by 01 and 0 safely
                    paired += dp1[adj] * total0;
                    paired += dp1[adj] * (total1 - dp1[adj]);
                    paired += dp1[adj] * (total10 - dp10[adj] - dp0[adj]);
                }
                else {
                    paired += dp0[adj] * (total01 - dp01[adj] - dp1[adj]);
                    paired += dp0[adj] * (total0 - dp0[adj]);
                }
                // single paths
                long single = 0;
                if (e.isOne) {
                    single += 2 * dp1[adj];
                    single += dp0[adj];
                    single += dp10[adj];
                }
                else {
                    single += 2 * dp0[adj];
                    single += dp1[adj];
                    single += dp01[adj];
                }
                ans += paired + single;
//                System.out.println("Added through " + adj + ": Paired: " + paired + " Single: " + single);
            }
        }
    }

    static class Edge {
        int v;
        boolean isOne;
        public Edge(int vv, boolean oo) {
            v = vv; isOne = oo;
        }
    }

    static class FastScanner {
        private int BS = 1<<16;
        private char NC = (char)0;
        private byte[] buf = new byte[BS];
        private int bId = 0, size = 0;
        private char c = NC;
        private double cnt = 1;
        private BufferedInputStream in;

        public FastScanner() {
            in = new BufferedInputStream(System.in, BS);
        }

        public FastScanner(String s) {
            try {
                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
            }
            catch (Exception e) {
                in = new BufferedInputStream(System.in, BS);
            }
        }

        private char getChar(){
            while(bId==size) {
                try {
                    size = in.read(buf);
                }catch(Exception e) {
                    return NC;
                }
                if(size==-1)return NC;
                bId=0;
            }
            return (char)buf[bId++];
        }

        public int nextInt() {
            return (int)nextLong();
        }

        public int[] nextInts(int N) {
            int[] res = new int[N];
            for (int i = 0; i < N; i++) {
                res[i] = (int) nextLong();
            }
            return res;
        }

        public long[] nextLongs(int N) {
            long[] res = new long[N];
            for (int i = 0; i < N; i++) {
                res[i] = nextLong();
            }
            return res;
        }

        public long nextLong() {
            cnt=1;
            boolean neg = false;
            if(c==NC)c=getChar();
            for(;(c<'0' || c>'9'); c = getChar()) {
                if(c=='-')neg=true;
            }
            long res = 0;
            for(; c>='0' && c <='9'; c=getChar()) {
                res = (res<<3)+(res<<1)+c-'0';
                cnt*=10;
            }
            return neg?-res:res;
        }

        public double nextDouble() {
            double cur = nextLong();
            return c!='.' ? cur:cur+nextLong()/cnt;
        }

        public String next() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c>32) {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public String nextLine() {
            StringBuilder res = new StringBuilder();
            while(c<=32)c=getChar();
            while(c!='\n') {
                res.append(c);
                c=getChar();
            }
            return res.toString();
        }

        public boolean hasNext() {
            if(c>32)return true;
            while(true) {
                c=getChar();
                if(c==NC)return false;
                else if(c>32)return true;
            }
        }
    }
}