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;
int n;
vector<pair<int, int> > son[200005];
long long valid = 0, One, Zero, OneZero, ZeroOne;
int mark[200005];
int sub[200005];
void fillsub(int u, int p) {
  sub[u] = 1;
  for (auto e : son[u]) {
    if (e.first == p) continue;
    if (mark[e.first]) continue;
    fillsub(e.first, u);
    sub[u] += sub[e.first];
  }
}
int FindCentroid(int u, int rootsub, int par) {
  int heavy = -1;
  for (auto e : son[u]) {
    if (e.first == par || mark[e.first]) continue;
    if (heavy == -1 || (sub[heavy] < sub[e.first])) {
      if (sub[e.first] > rootsub / 2) heavy = e.first;
    }
  }
  if (heavy == -1) {
    return u;
  } else {
    return FindCentroid(heavy, rootsub, u);
  }
}
void dfszero(int u, int p, int lc, int add) {
  if (lc == 0)
    Zero += add;
  else
    OneZero += add;
  for (auto e : son[u]) {
    int v = e.first, c = e.second;
    if (v == p || mark[v]) continue;
    if (c < lc) continue;
    dfszero(v, u, c, add);
  }
}
void dfsone(int u, int p, int lc, int add) {
  if (lc == 1)
    One += add;
  else
    ZeroOne += add;
  for (auto e : son[u]) {
    int v = e.first, c = e.second;
    if (v == p || mark[v]) continue;
    if (c > lc) continue;
    dfsone(v, u, c, add);
  }
}
void queryzero(int u, int p, int lc) {
  if (lc == 0)
    valid += Zero + Zero + One + OneZero;
  else
    valid += Zero;
  for (auto e : son[u]) {
    int v = e.first, c = e.second;
    if (v == p || mark[v]) continue;
    if (c < lc) continue;
    queryzero(v, u, c);
  }
}
void queryone(int u, int p, int lc) {
  if (lc == 1)
    valid += One + One + Zero + ZeroOne;
  else
    valid += One;
  for (auto e : son[u]) {
    int v = e.first, c = e.second;
    if (v == p || mark[v]) continue;
    if (c > lc) continue;
    queryone(v, u, c);
  }
}
void solve(int u) {
  One = Zero = OneZero = ZeroOne = 0;
  for (int i = 0; i < son[u].size(); i++) {
    int v = son[u][i].first, c = son[u][i].second;
    if (mark[v]) continue;
    if (c == 0)
      queryzero(v, u, 0);
    else
      queryone(v, u, 1);
    if (c == 0)
      dfszero(v, u, 0, 1);
    else
      dfsone(v, u, 1, 1);
  }
  valid += (One * 2) + (Zero * 2) + OneZero + ZeroOne;
}
int Universe(int root) {
  fillsub(root, -1);
  int cent = FindCentroid(root, sub[root], -1);
  solve(cent);
  mark[cent] = true;
  for (auto e : son[cent]) {
    if (mark[e.first]) continue;
    int child = Universe(e.first);
  }
  return cent;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    son[u].push_back(make_pair(v, c));
    son[v].push_back(make_pair(u, c));
  }
  int cent = Universe(1);
  printf("%lld\n", valid);
  return 0;
}
