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;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const long long INF = 0x3f3f3f3f;
const double eps = 1e-9;
const long long mod = 1e9 + 7;
vector<vector<vector<long long>>> g;
vector<vector<long long>> sz, ccs;
void DFS(long long gi, long long node, long long cc) {
  ccs[gi][node] = cc;
  sz[gi][cc]++;
  for (long long v : g[gi][node]) {
    if (ccs[gi][v] == -1) {
      DFS(gi, v, cc);
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  g.assign(2, vector<vector<long long>>(n));
  sz.assign(2, vector<long long>(n));
  ccs.assign(2, vector<long long>(n, -1));
  for (long long i = 0; i < n - 1; i++) {
    long long u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    g[c][u].emplace_back(v);
    g[c][v].emplace_back(u);
  }
  long long ans = 0, cc0 = 0, cc1 = 0;
  for (long long i = 0; i < n; i++) {
    if (ccs[0][i] == -1) {
      DFS(0, i, cc0++);
    }
    if (ccs[1][i] == -1) {
      DFS(1, i, cc1++);
    }
    long long x = ccs[0][i];
    long long y = ccs[1][i];
    ans += sz[0][x] * sz[1][y];
  }
  ans -= n;
  cout << ans << "\n";
  return 0;
}
