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.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jenish
 */
public class Main {
    public static void main(String[] args) {
        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[][] arr;
        long[][] size;

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();

            size = new long[2][n + 1];
            Arrays.fill(size[0], 1);
            Arrays.fill(size[1], 1);
            arr = new int[2][n + 1];
            for (int i = 1; i <= n; i++) arr[0][i] = i;
            for (int i = 1; i <= n; i++) arr[1][i] = i;
            for (int i = 0; i < n - 1; i++) {
                int a = in.scanInt();
                int b = in.scanInt();
                int c = in.scanInt();
                if (root(a, c) != root(b, c)) w_union(a, b, c);
            }

            long ans = 0;
            for (int i = 1; i <= n; i++) {
                ans += size[0][i] * (size[0][i] - 1);
                ans += size[1][i] * (size[1][i] - 1);
                ans += (size[0][root(i, 0)] - 1) * (size[1][root(i, 1)] - 1);
            }
            out.println(ans);


        }

        int root(int i, int index) {
            while (arr[index][i] != i) {
                i = arr[index][i];
            }
            return i;
        }

        void w_union(int A, int B, int c) {
            int root_A = root(A, c);
            int root_B = root(B, c);
            if (size[c][root_A] < size[c][root_B]) {
                arr[c][root_A] = arr[c][root_B];
                size[c][root_B] += size[c][root_A];
                size[c][root_A] = 0;
            } else {
                arr[c][root_B] = arr[c][root_A];
                size[c][root_A] += size[c][root_B];
                size[c][root_B] = 0;
            }
        }

    }

    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 I = 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') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

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

    }
}

