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

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

    static class D01Tree {
        static D01Tree.Node[] graph0;
        static D01Tree.Node[] graph1;
        static boolean[] visited0;
        static boolean[] visited1;
        static ArrayList<Integer> comp0;
        static ArrayList<Integer> comp1;
        static int size;

        public void solve(int testNumber, inputClass sc, PrintWriter out) {
            int n = sc.nextInt();
            int x, y, w;
            graph0 = new D01Tree.Node[n];
            graph1 = new D01Tree.Node[n];
            visited0 = new boolean[n];
            visited1 = new boolean[n];
            comp0 = new ArrayList<>();
            comp1 = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                graph0[i] = new D01Tree.Node();
                graph0[i].edges = new ArrayList<>();
                graph1[i] = new D01Tree.Node();
                graph1[i].edges = new ArrayList<>();
            }
            for (int i = 0; i < n - 1; i++) {
                x = sc.nextInt() - 1;
                y = sc.nextInt() - 1;
                w = sc.nextInt();
                if (w == 1) {
                    graph1[x].edges.add(y);
                    graph1[y].edges.add(x);
                } else if (w == 0) {
                    graph0[x].edges.add(y);
                    graph0[y].edges.add(x);
                }
            }

            for (int i = 0; i < n; i++) {
                if (!visited0[i]) {
                    size = 0;
                    dfs0(i, comp0.size());
                    comp0.add(size);
                }
            }
            for (int i = 0; i < n; i++) {
                if (!visited1[i]) {
                    size = 0;
                    dfs1(i, comp1.size());
                    comp1.add(size);
                }
            }

            long ans = 0;
            for (int i = 0; i < comp0.size(); i++) {
                ans += (long) comp0.get(i) * (long) (comp0.get(i) - 1);
            }
            for (int i = 0; i < comp1.size(); i++) {
                ans += (long) comp1.get(i) * (long) (comp1.get(i) - 1);
            }

            for (int i = 0; i < n; i++) {
                if (comp0.get(graph0[i].comp) > 1 && comp1.get(graph1[i].comp) > 1) {
                    ans += (long) (comp0.get(graph0[i].comp) - 1) * (long) (comp1.get(graph1[i].comp) - 1);
                }
            }
            out.println(ans);
        }

        private void dfs0(int a, int c) {
            visited0[a] = true;
            graph0[a].comp = c;
            size++;
            for (int i : graph0[a].edges) {
                if (!visited0[i])
                    dfs0(i, c);
            }
        }

        private void dfs1(int a, int c) {
            visited1[a] = true;
            graph1[a].comp = c;
            size++;
            for (int i : graph1[a].edges) {
                if (!visited1[i])
                    dfs1(i, c);
            }
        }

        static class Node {
            ArrayList<Integer> edges;
            int comp;

        }

    }

    static class inputClass {
        BufferedReader br;
        StringTokenizer st;

        public inputClass(InputStream in) {

            br = new BufferedReader(new InputStreamReader(in));
        }

        public String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    }
}

