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;
long long MOD = (long long)1000000007;
long long ans = 0, zzo, o;
vector<vector<pair<int, char>>> Graph;
vector<int> sz, vec;
vector<bool> visited, centroidMarked;
int dfs1(int v, int depth, int parent) {
  int curr = 1;
  visited[v] = true;
  for (auto vv : Graph[v])
    if (!visited[vv.first]) curr += dfs1(vv.first, depth + 1, v);
  return sz[v] = curr;
}
int dfs2(int v, int parent, char last, char first, bool ok) {
  if (ok) {
    if (first == '1')
      ++o;
    else
      ++zzo;
  }
  int curr = 1;
  for (auto vv : Graph[v])
    if (!centroidMarked[vv.first] && vv.first != parent) {
      bool bol = ok;
      if ((last == '1' && vv.second == '0')) bol = false;
      curr += dfs2(vv.first, v, vv.second, first, bol);
    }
  return sz[v] = curr;
}
int getCentroid(int v, int parent, int subTreeSize) {
  for (auto vv : Graph[v])
    if (!centroidMarked[vv.first] && vv.first != parent &&
        sz[vv.first] > subTreeSize / 2)
      return getCentroid(vv.first, v, subTreeSize);
  return v;
}
void add(int v, int parent, int sign, char last, char first) {
  if (first == '1')
    o += sign;
  else
    zzo += sign;
  for (auto vv : Graph[v])
    if (!centroidMarked[vv.first] && vv.first != parent &&
        !(last == '1' && vv.second == '0'))
      add(vv.first, v, sign, vv.second, first);
}
void solve(int v, int parent, char last, char first) {
  if (first == '0')
    ans += (o + zzo);
  else
    ans += o;
  ++ans;
  for (auto vv : Graph[v])
    if (!centroidMarked[vv.first] && vv.first != parent &&
        !(last == '0' && vv.second == '1'))
      solve(vv.first, v, vv.second, first);
}
void decompose(int v, int n, int subTreeSize) {
  int centroid = getCentroid(v, -1, subTreeSize), i = vec.size();
  centroidMarked[centroid] = true;
  zzo = o = 0;
  for (auto vv : Graph[centroid])
    if (!centroidMarked[vv.first]) {
      int newSubTreeSize = dfs2(vv.first, centroid, vv.second, vv.second, true);
      vec.push_back(newSubTreeSize);
    }
  ans += (zzo + o);
  for (auto vv : Graph[centroid])
    if (!centroidMarked[vv.first]) {
      add(vv.first, centroid, -1, vv.second, vv.second);
      solve(vv.first, centroid, vv.second, vv.second);
      add(vv.first, centroid, 1, vv.second, vv.second);
    }
  for (auto vv : Graph[centroid])
    if (!centroidMarked[vv.first]) decompose(vv.first, n, vec[i++]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, u, v;
  char c;
  cin >> n;
  Graph.resize(n + 5), sz.resize(n + 5), visited.resize(n + 5),
      centroidMarked.resize(n + 5);
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v >> c;
    Graph[u].push_back({v, c});
    Graph[v].push_back({u, c});
  }
  dfs1(1, 0, 0);
  decompose(1, n, n);
  cout << ans;
}
