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;
template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << p.first << " " << p.second;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> const &v) {
  long long int l = v.size();
  for (long long int i = 0; i < l - 1; i++) out << v[i] << ' ';
  if (l > 0) out << v[l - 1];
  return out;
}
template <typename T>
void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}
vector<pair<long long, long long> > adj[200000];
long long int dp[200000][2];
long long int ans = 0;
long long int par[200000];
long long int upw[200000];
void dfs(long long int second, long long int p, long long int w) {
  par[second] = p;
  upw[second] = w;
  for (auto &e : adj[second]) {
    long long int v = e.first;
    long long int par = e.second;
    if (v == p) continue;
    dfs(v, second, par);
    if (par == 0) {
      dp[second][0] += dp[v][0] + dp[v][1];
      dp[second][0]++;
    } else {
      dp[second][1] += dp[v][1];
      dp[second][1]++;
    }
  }
  ans += dp[second][0] + dp[second][1];
}
long long int dpup[200000][2];
void dfsup(long long int second, long long int p, long long int w) {
  if (p != -1) {
    if (w == 1) {
      dpup[second][1] = dp[p][1] - dp[second][1] - 1;
      dpup[second][1]++;
      dpup[second][1] += dpup[p][1];
    } else {
      dpup[second][0] += dpup[p][0] + dpup[p][1];
      dpup[second][0]++;
      dpup[second][0] +=
          dp[p][0] + dp[p][1] - dp[second][0] - dp[second][1] - 1;
    }
  }
  ans += dpup[second][0] + dpup[second][1];
  for (auto &e : adj[second]) {
    if (e.first != p) {
      dfsup(e.first, second, e.second);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  for (long long int i = 0; i < n - 1; i++) {
    long long int u, v;
    cin >> u >> v;
    u--;
    v--;
    long long int w;
    cin >> w;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  dfs(0, -1, -1);
  dfsup(0, -1, -1);
  cout << ans << "\n";
  return 0;
}
