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>
/**
 * Created by Aminul on 5/16/2019.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class D_2 {
    static boolean vis[], mark[];
    static int g[][][];
    static long count[][], ans[][];

    public static void main(String[] args) throws Exception {
        FastReader in = new FastReader(System.in);
        PrintWriter pw = new PrintWriter(System.out);

        int n = in.nextInt();
        count = new long[2][2];
        ans = new long[2][2];
        int from[] = new int[n - 1], to[] = new int[n - 1], w[] = new int[n - 1];
        for (int i = 0; i < n - 1; i++) {
            int u = in.nextInt(), v = in.nextInt(), c = in.nextInt();
            from[i] = u;
            to[i] = v;
            w[i] = c;
        }
        int ug[][] = packU(n + 1, from, to);
        g = packWU(n + 1, from, to, w);
        int centroids[] = new Centroid_Decomposition(ug, 1).centroids;
        vis = new boolean[n + 1];
        mark = new boolean[n + 1];
        for (int u : centroids) {
            mark[u] = true;
            vis[u] = true;
            for (int v[] : g[u]) {
                dfs(v[0], v[1], v[1], +1);
            }
            ans[0][0] += count[0][0];
            ans[0][1] += count[0][1];
            ans[1][1] += count[1][1];
            for (int v[] : g[u]) {
                dfs(v[0], v[1], v[1], -1);
                calc(v[0], v[1], v[1]);
                dfs(v[0], v[1], v[1], +1);
            }
            for (int v[] : g[u]) {
                dfs(v[0], v[1], v[1], -1);
            }
            vis[u] = false;
        }

        long res = ans[0][0] + ans[0][1] + ans[1][1];
        pw.println(res);

        pw.close();
    }

    static void dfs(int u, int start, int curr, int val) {
        vis[u] = true;
        count[start][curr] += val;
        if (mark[u]) {
            vis[u] = false;
            return;
        }
        for (int v[] : g[u]) {
            int node = v[0], w = v[1];
            if (curr == 1 && w == 0) continue;
            if (vis[node]) continue;
            if (mark[node]) {
                count[start][curr | w] += val;
                continue;
            }
            dfs(node, start, curr | w, val);
        }
        vis[u] = false;
    }

    static void calc(int u, int start, int curr) {
        if (mark[u]) return;
        vis[u] = true;
        if (curr == 0) {
            if (start == 0) {
                ans[0][0] += count[0][0];
                ans[0][1] += count[0][1];
                ans[0][1] += count[1][1];
            } else {
                ans[0][1] += count[1][1];
            }
        } else {
            ans[1][1] += count[1][1];
        }

        for (int v[] : g[u]) {
            int node = v[0], w = v[1];
            if (mark[node] || vis[node]) continue;
            if (curr == 0 && w == 1) continue;
            calc(node, start, curr & w);
        }
        vis[u] = false;
    }

    public static int[][][] packWU(int n, int[] from, int[] to, int[] w) {
        int[][][] g = new int[n][][];
        int[] p = new int[n];
        for (int f : from)
            p[f]++;
        for (int t : to)
            p[t]++;
        for (int i = 0; i < n; i++)
            g[i] = new int[p[i]][2];
        for (int i = 0; i < from.length; 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 int[][] packU(int n, int[] from, int[] to) {
        int[][] g = new int[n][];
        int[] p = new int[n];
        for (int f : from)
            p[f]++;
        for (int t : to)
            p[t]++;
        for (int i = 0; i < n; i++)
            g[i] = new int[p[i]];
        for (int i = 0; i < from.length; i++) {
            g[from[i]][--p[from[i]]] = to[i];
            g[to[i]][--p[to[i]]] = from[i];
        }
        return g;
    }

    static void debug(Object... obj) {
        System.err.println(Arrays.deepToString(obj));
    }

    static class Centroid_Decomposition {
        int g[][];
        int[] size, par;
        boolean[] vis;
        int centroids[], cd;

        Centroid_Decomposition(int g[][], int root) { // 1-based indexed, root = 1
            this.g = g;
            int n = g.length;
            size = new int[n];
            par = new int[n];
            vis = new boolean[n];
            centroids = new int[n - 1];
            cd = 0;
            calcSize(root, 0);
            decompose(root, 0);
        }

        int calcSize(int u, int p) {
            size[u] = 1;
            for (int v : g[u]) {
                if (v != p) {
                    size[u] += calcSize(v, u);
                }
            }
            return size[u];
        }

        void decompose(int u, int p) {
            int max = 0, v = -1;
            for (int x : g[u]) {
                if (!vis[x] && size[x] > max) {
                    max = size[x];
                    v = x;
                }
            }
            while (max > size[u] / 2) {
                int t = size[v];
                size[v] = size[u];
                size[u] = size[v] - t;
                u = v;
                max = 0;
                for (int x : g[u]) {
                    if (!vis[x] && size[x] > max) {
                        max = size[x];
                        v = x;
                    }
                }
            }
            centroids[cd++] = u;
            par[u] = p;
            vis[u] = true;
            for (int x : g[u]) {
                if (!vis[x]) {
                    decompose(x, u);
                }
            }
        }

        void update(int u) {
            for (int v = u; v != 0; v = par[v]) {
                //int dist = lca.dist(u, v);
                //nearest[v] = min(nearest[v], dist);
                //do update
            }
        }

        int query(int u) {
            int min = Integer.MAX_VALUE;
            for (int v = u; v != 0; v = par[v]) {
                //int dist = lca.dist(u, v);
                //min = min(min, dist + nearest[v]);
                //do query
            }
            return min;
        }

    }

    static class FastReader {
        InputStream is;
        private byte[] inbuf = new byte[1024];
        private int lenbuf = 0, ptrbuf = 0;

        public FastReader(InputStream is) {
            this.is = is;
        }

        public int readByte() {
            if (lenbuf == -1) throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = is.read(inbuf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0) return -1;
            }
            return inbuf[ptrbuf++];
        }

        public boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public int skip() {
            int b;
            while ((b = readByte()) != -1 && isSpaceChar(b)) ;
            return b;
        }

        public String next() {
            int b = skip();
            StringBuilder sb = new StringBuilder();
            while (!(isSpaceChar(b))) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }


        public String nextLine() {
            int c = skip();
            StringBuilder sb = new StringBuilder();
            while (!isEndOfLine(c)) {
                sb.appendCodePoint(c);
                c = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

        public long nextLong() {
            long num = 0;
            int b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = (num << 3) + (num << 1) + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public char[] next(int n) {
            char[] buf = new char[n];
            int b = skip(), p = 0;
            while (p < n && !(isSpaceChar(b))) {
                buf[p++] = (char) b;
                b = readByte();
            }
            return n == p ? buf : Arrays.copyOf(buf, p);
        }

        public char readChar() {
            return (char) skip();
        }
    }
}