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;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
const long long mod = (long long)1e9 + 7;
const long long hell = mod;
inline long long add(long long a, long long b) {
  a += b;
  if (a >= mod) a -= mod;
  return a;
}
inline long long sub(long long a, long long b) {
  a -= b;
  if (a < 0) a += mod;
  return a;
}
inline long long mul(long long a, long long b) { return (a * 1ll * b) % mod; }
inline long long powmod(long long a, long long b) {
  long long rt = 1;
  while (b > 0) {
    if (b & 1) rt = mul(rt, a);
    a = mul(a, a);
    b >>= 1;
  }
  return rt;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const long long N = (long long)2e5 + 7;
long long n;
vector<vector<long long> > adj;
long long p[N][2];
long long siz[N][2];
void make_set(long long v) {
  p[v][0] = p[v][1] = v;
  siz[v][0] = siz[v][1] = 1;
}
long long find_set(long long v, long long x) {
  if (v == p[v][x]) return v;
  return p[v][x] = find_set(p[v][x], x);
}
void merge(long long a, long long b, long long x) {
  a = find_set(a, x);
  b = find_set(b, x);
  if (a != b) {
    if (siz[a][x] < siz[b][x]) swap(a, b);
    p[b][x] = a;
    siz[a][x] += siz[b][x];
  }
}
void solve() {
  cin >> n;
  adj.resize(n);
  for (long long i = 0; i < n; i++) make_set(i);
  for (long long i = 0; i < n - 1; i++) {
    long long x, y;
    long long xx;
    cin >> x >> y;
    cin >> xx;
    x--, y--;
    merge(x, y, xx);
  }
  long long final = 0;
  for (long long i = 0; i < n; i++) {
    if (p[i][0] == i) {
      final += siz[i][0] * (siz[i][0] - 1);
    }
    if (p[i][1] == i) {
      final += siz[i][1] * (siz[i][1] - 1);
    }
    final += (siz[find_set(i, 0)][0] - 1) * (siz[find_set(i, 1)][1] - 1);
  }
  cout << final << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL),
      cout << setprecision(14);
  ;
  long long t = 1;
  while (t--) {
    solve();
  }
}
