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<long long int, long long int>> adj[200001];
long long int ifzero[200001], any[200001], parent[200001], actual[200001][2];
long long int vis[200001];
vector<long long int> levels[200001];
void dfs(long long int src, long long int par, long long int le) {
  vis[src] = 1;
  levels[le].push_back(src);
  long long int u = src;
  parent[u] = par;
  for (auto it : adj[u]) {
    long long int v = it.first;
    long long int z = it.second;
    if (!vis[v]) {
      dfs(v, u, le + 1);
      if (z == 1) {
        ifzero[u] += ifzero[v] + 1;
      } else {
        any[u] += any[v] + ifzero[v] + 1;
      }
    }
  }
}
void solve() {
  long long int n, i, j, k, l, m, o, p, q, r, s, t;
  cin >> n;
  map<pair<long long int, long long int>, long long int> mp;
  for (i = 0; i < n - 1; i++) {
    cin >> j >> k >> l;
    adj[j].push_back({k, l});
    adj[k].push_back({j, l});
    mp[{j, k}] = l;
    mp[{k, j}] = l;
  }
  memset(vis, 0, sizeof(vis));
  memset(actual, 0, sizeof(vis));
  memset(any, 0, sizeof(vis));
  memset(ifzero, 0, sizeof(vis));
  dfs(1, 0, 0);
  for (i = 1; i <= n; i++) {
  }
  actual[1][0] = any[1];
  actual[1][1] = ifzero[1];
  for (i = 1; i <= n - 1; i++) {
    for (j = 0; j < levels[i].size(); j++) {
      long long int u = levels[i][j];
      long long int v = parent[u];
      long long int z = mp[{u, v}];
      actual[u][0] = any[u];
      actual[u][1] = ifzero[u];
      if (z == 1) {
        actual[u][1] += actual[v][1] - ifzero[u];
      } else {
        actual[u][0] += actual[v][0] + actual[v][1] - ifzero[u] - any[u];
      }
    }
  }
  long long int ans = 0;
  for (i = 1; i <= n; i++) {
    ans += actual[i][0] + actual[i][1];
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
