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;
vector<pair<int, int> > edge[200005];
int par_one[200005], par_zero[200005];
int cnt_one[200005], cnt_zero[200005];
int find_p_one(int x) {
  if (par_one[x] != x) par_one[x] = find_p_one(par_one[x]);
  return par_one[x];
}
void merg_one(int x, int y) { par_one[find_p_one(y)] = find_p_one(x); }
int find_p_zero(int x) {
  if (par_zero[x] != x) par_zero[x] = find_p_zero(par_zero[x]);
  return par_zero[x];
}
void merg_zero(int x, int y) { par_zero[find_p_zero(y)] = find_p_zero(x); }
int dfs_one(int node, int par) {
  int ans = 1;
  int other, weight;
  for (int i = 0; i < edge[node].size(); i++) {
    other = edge[node][i].first;
    weight = edge[node][i].second;
    if (other != par && weight == 1) {
      merg_one(node, other);
      ans += dfs_one(other, node);
    }
  }
  cnt_one[node] = ans;
  return ans;
}
int dfs_zero(int node, int par) {
  int ans = 1;
  int other, weight;
  for (int i = 0; i < edge[node].size(); i++) {
    other = edge[node][i].first;
    weight = edge[node][i].second;
    if (other != par && weight == 0) {
      merg_zero(node, other);
      ans += dfs_zero(other, node);
    }
  }
  cnt_zero[node] = ans;
  return ans;
}
long long C2(int x) {
  long long ans = (long long)x * (long long)(x - 1);
  return ans;
}
int main() {
  ios_base ::sync_with_stdio(false);
  int n;
  int x, y, w;
  cin >> n;
  for (int i = 0; i < n; i++) {
    par_one[i] = par_zero[i] = i;
    cnt_one[i] = cnt_zero[i] = 1;
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y >> w;
    edge[x].push_back(pair<int, int>(y, w));
    edge[y].push_back(pair<int, int>(x, w));
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (par_one[i] == i) {
      dfs_one(i, i);
      ans += C2(cnt_one[i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (par_zero[i] == i) {
      dfs_zero(i, i);
      ans += C2(cnt_zero[i]);
    }
  }
  int p0, p1;
  for (int i = 1; i <= n; i++) {
    p0 = find_p_zero(i);
    p1 = find_p_one(i);
    ans += (long long)(cnt_one[p1] - 1) * (long long)(cnt_zero[p0] - 1);
  }
  cout << ans << endl;
  return 0;
}
