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.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author prakharjain
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        D01Tree solver = new D01Tree();
        solver.solve(1, in, out);
        out.close();
    }

    static class D01Tree {
        List[] g;
        long[][][] dp;
        long ans = 0;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();

            g = new List[n];

            for (int i = 0; i < n; i++) {
                g[i] = new ArrayList();
            }

            for (int i = 0; i < n - 1; i++) {
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;

                int c = in.nextInt();


                g[u].add(new D01Tree.edge(v, c));
                g[v].add(new D01Tree.edge(u, c));
            }

            dp = new long[n][2][2];

            dfs(0, -1);

//        long ans = 0;
//
//        for (int i = 0; i < n; i++) {
//            for (int j = 0; j < 2; j++) {
//                for (int k = 0; k < 2; k++) {
//                    ans += dp[i][j][k];
//                    if (j == k) {
//                        ans += dp[i][j][k];
//                    }
//                }
//            }
//        }

            out.println(ans);
        }

        void dfs(int u, int p) {

            long zo = 0;
            long oo = 0;
            long zz = 0;
            long oz = 0;
            for (int i = 0; i < g[u].size(); i++) {
                D01Tree.edge edge = (D01Tree.edge) g[u].get(i);

                if (edge.v == p)
                    continue;

                dfs(edge.v, u);

                if (edge.c == 0) {
                    ans += 2 * (dp[edge.v][0][0] + 1) * zz;
                    ans += (dp[edge.v][0][0] + 1) * (zo + oo);
                    ans += (dp[edge.v][0][1] + dp[edge.v][1][1]) * zz;
                    dp[u][0][0] += dp[edge.v][0][0] + 1;
                    dp[u][0][1] += dp[edge.v][0][1] + dp[edge.v][1][1];
                    zz += dp[edge.v][0][0] + 1;
                    zo += dp[edge.v][0][1] + dp[edge.v][1][1];
                } else {
                    ans += 2 * (dp[edge.v][1][1] + 1) * oo;
                    ans += (dp[edge.v][1][1] + 1) * (oz + zz);
                    ans += (dp[edge.v][1][0] + dp[edge.v][0][0]) * oo;
                    dp[u][1][0] += dp[edge.v][1][0] + dp[edge.v][0][0];
                    dp[u][1][1] += dp[edge.v][1][1] + 1;
                    oz += dp[edge.v][1][0] + dp[edge.v][0][0];
                    oo += dp[edge.v][1][1] + 1;
                }
            }

            ans += 2 * dp[u][0][0];
            ans += 2 * dp[u][1][1];
            ans += dp[u][0][1];
            ans += dp[u][1][0];
        }

        static class edge {
            int v;
            int c;

            public edge(int v, int c) {
                this.v = v;
                this.c = c;
            }

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(long i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

