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>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e5 + 5;
int n;
struct DSU {
  vector<int> p, d;
  DSU(int n) {
    p = vector<int>(n + 1, -1);
    d = vector<int>(n + 1, 0);
  }
  int Find(int x) { return p[x] < 0 ? x : p[x] = Find(p[x]); }
  void Union(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y) return;
    if (rand() % 2) swap(x, y);
    p[x] += p[y];
    d[x] += d[y];
    p[y] = x;
  }
};
struct Edge {
  int to, w;
};
vector<Edge> adj[maxn];
ll ch2(ll x) { return x * (x - 1); }
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    adj[u].push_back({v, c});
    adj[v].push_back({u, c});
  }
  vector<DSU> dsu = {DSU(n), DSU(n)};
  for (int i = 1; i <= n; i++) {
    for (Edge e : adj[i]) {
      int f = e.w == 0 ? 1 : 0;
      dsu[f].d[i]++;
    }
    for (Edge e : adj[i]) {
      dsu[e.w].Union(i, e.to);
    }
  }
  ll ans = 0;
  set<int> f[2];
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      int par = dsu[j].Find(i);
      if (f[j].count(par)) continue;
      f[j].insert(par);
      int sz = -dsu[j].p[par];
      ans += ch2(sz);
    }
  }
  for (int i = 1; i <= n; i++) {
    int p1 = dsu[0].Find(i);
    int s1 = -dsu[0].p[p1];
    int p2 = dsu[1].Find(i);
    int s2 = -dsu[1].p[p2];
    ans += 1LL * (s1 - 1) * (s2 - 1);
  }
  cout << ans << '\n';
  return 0;
}
