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.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D01Derevo solver = new D01Derevo();
        solver.solve(1, in, out);
        out.close();
    }

    static class D01Derevo {

        long ans = 0;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            D01Derevo.Vertex[] vs = D01Derevo.Vertex.emptyGraph(n);
            for (int i = 1; i < n; i++) {
                int a = in.nextInt() - 1;
                int b = in.nextInt() - 1;
                int color = in.nextInt();
                vs[a].adj.add(new D01Derevo.Edge(vs[b], color));
                vs[b].adj.add(new D01Derevo.Edge(vs[a], color));
            }
            ans = 0;
            doit(vs[0], null);
            out.println(ans);
        }

        private void doit(D01Derevo.Vertex v, D01Derevo.Vertex prev) {
            for (D01Derevo.Edge e : v.adj) {
                if (e.to == prev) {
                    continue;
                }
                doit(e.to, v);
            }
            long ones = 0;
            long zeros = 0;
            long zeroOnes = 0;
            long oneZeros = 0;
            for (D01Derevo.Edge e : v.adj) {
                if (e.to == prev) {
                    continue;
                }
                if (e.color == 1) {
                    ones += e.to.ones + 1;
                    zeroOnes += e.to.zeroOnes + e.to.zeros;
                } else {
                    zeros += e.to.zeros + 1;
                    oneZeros += e.to.oneZeros + e.to.ones;
                }
            }
            v.ones = ones;
            v.zeros = zeros;
            v.zeroOnes = zeroOnes;
            v.oneZeros = oneZeros;

            ans += 2 * ones + 2 * zeros + zeroOnes + oneZeros;
            for (D01Derevo.Edge e : v.adj) {
                if (e.to == prev) {
                    continue;
                }
                if (e.color == 1) {
                    ans += (e.to.ones + e.to.zeroOnes + e.to.zeros + 1) * (ones - e.to.ones - 1);
                } else {
                    ans += (e.to.zeros + 1) * (zeros - e.to.zeros - 1 + oneZeros - e.to.oneZeros - e.to.ones + ones);
                }
            }
        }

        public static class Vertex {

            long ones = -1;
            long zeros = -1;
            long zeroOnes = -1;
            long oneZeros = -1;
            int idx;
            List<D01Derevo.Edge> adj = new ArrayList<>();

            public static D01Derevo.Vertex[] emptyGraph(int n) {
                D01Derevo.Vertex[] vs = new D01Derevo.Vertex[n];
                for (int i = 0; i < n; i++) {
                    vs[i] = new D01Derevo.Vertex();
                    vs[i].idx = i;
                }
                return vs;
            }
        }

        public static class Edge {

            D01Derevo.Vertex to;
            int color;

            public Edge(D01Derevo.Vertex to, int color) {
                this.to = to;
                this.color = color;
            }
        }
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

