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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D01Tree solver = new D01Tree();
        solver.solve(1, in, out);
        out.close();
    }

    static class D01Tree {
        int[][][] G;
        long[][] dp;
        long ans;
    //
        public void findIt(int n, int p) {
            for (int[] i : G[n]) {
                if (i[0] == p) continue;
                findIt(i[0], n);
                if (i[1] == 1) {
                    dp[n][1] += dp[i[0]][1] + 1;
                    dp[n][3] += (dp[i[0]][3] + 1 + dp[i[0]][0]);
                } else {
                    dp[n][0] += dp[i[0]][0] + 1;
                    dp[n][2] += (dp[i[0]][2] + 1 + dp[i[0]][1]);
                }
            }
            ans += (dp[n][0] * 2l);
            ans += (dp[n][1] * 2l);
            ans += dp[n][2] - dp[n][0];
            ans += dp[n][3] - dp[n][1];


            for (int[] i : G[n]) {
                if (i[0] == p) continue;
                if (i[1] == 1) {
                    dp[n][1] -= dp[i[0]][1] + 1;
                    dp[n][3] -= (dp[i[0]][3] + 1 + dp[i[0]][0]);
                } else {
                    dp[n][0] -= dp[i[0]][0] + 1;
                    dp[n][2] -= (dp[i[0]][2] + 1 + dp[i[0]][1]);
                }


                if (i[1] == 0) {
                    ans += ((dp[i[0]][0] + 1l) * (dp[n][0]));
                    ans += ((dp[i[0]][0] + 1l) * (dp[n][2] - dp[n][0]));
                    ans += ((dp[i[0]][0] + 1l) * (dp[n][1]));
                }
                if (i[1] == 1) {
                    ans += ((dp[i[0]][3] + 1l + dp[i[0]][0]) * (dp[n][1]));
                }


                if (i[1] == 1) {
                    dp[n][1] += dp[i[0]][1] + 1;
                    dp[n][3] += (dp[i[0]][3] + 1 + dp[i[0]][0]);
                } else {
                    dp[n][0] += dp[i[0]][0] + 1;
                    dp[n][2] += (dp[i[0]][2] + 1 + dp[i[0]][1]);
                }
            }
        }

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int[] from = new int[n - 1];
            int[] to = new int[n - 1];
            int[] weight = new int[n - 1];
            for (int i = 0; i < n - 1; i++) {
                from[i] = in.scanInt();
                to[i] = in.scanInt();
                weight[i] = in.scanInt();
            }
            dp = new long[n + 1][4];
            G = CodeHash.packGraphW(from, to, weight, n);
            ans = 0;
            findIt(1, -1);
            out.println(ans);

        }

    }

    static class CodeHash {
        public static int[][][] packGraphW(int[] from, int[] to, int[] w, int n) {
            return packGraphW(from, to, w, n, from.length);
        }

        public static int[][][] packGraphW(int[] from, int[] to, int[] w, int n, int m) {
            int[][][] g = new int[n + 1][][];
            int[] p = new int[n + 1];
            for (int i = 0; i < m; i++) p[from[i]]++;
            for (int i = 0; i < m; i++) p[to[i]]++;
            for (int i = 0; i <= n; i++) g[i] = new int[p[i]][2];
            for (int i = 0; i < m; i++) {
                --p[from[i]];
                g[from[i]][p[from[i]]][0] = to[i];
                g[from[i]][p[from[i]]][1] = w[i];
                --p[to[i]];
                g[to[i]][p[to[i]]][0] = from[i];
                g[to[i]][p[to[i]]][1] = w[i];
            }
            return g;
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}

