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>
inline long long gi() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
int fir[200010], dis[400010], nxt[400010], id;
bool w[400010];
inline void link(int a, int b, bool c) {
  nxt[++id] = fir[a], fir[a] = id, dis[id] = b, w[id] = c;
}
int N, f[200010], siz[200010], rt;
bool vis[200010];
inline void getrt(int x, int fa = -1) {
  siz[x] = 1, f[x] = 0;
  for (int i = fir[x]; i; i = nxt[i]) {
    if (dis[i] == fa || vis[dis[i]]) continue;
    getrt(dis[i], x);
    siz[x] += siz[dis[i]];
    f[x] = std::max(f[x], siz[dis[i]]);
  }
  f[x] = std::max(f[x], N - siz[x]);
  if (f[rt] > f[x]) rt = x;
}
long long ans = 0;
int tot[2];
inline void dfs(int x, bool y, bool f, bool c, int fa = -1) {
  ++tot[f];
  if (c) ++ans;
  for (int i = fir[x]; i; i = nxt[i]) {
    if (vis[dis[i]] || dis[i] == fa) continue;
    if (w[i] && !y) continue;
    dfs(dis[i], w[i], f, c, x);
  }
}
inline void DFS(int x, bool y, bool f, int fa = -1) {
  ans += tot[0] + f * tot[1];
  for (int i = fir[x]; i; i = nxt[i]) {
    if (vis[dis[i]] || dis[i] == fa) continue;
    if (!w[i] && y) continue;
    DFS(dis[i], w[i], f, x);
  }
}
inline void solve(int x) {
  vis[x] = 1;
  std::vector<std::pair<int, bool>> son;
  for (int i = fir[x]; i; i = nxt[i])
    if (!vis[dis[i]]) son.push_back({dis[i], w[i]});
  tot[0] = 1, tot[1] = 0;
  for (auto i : son)
    DFS(i.first, i.second, i.second), dfs(i.first, i.second, i.second, 1);
  std::reverse(son.begin(), son.end());
  tot[0] = tot[1] = 0;
  for (auto i : son)
    DFS(i.first, i.second, i.second), dfs(i.first, i.second, i.second, 0);
  for (auto i : son) rt = 0, N = siz[i.first], getrt(i.first), solve(rt);
}
int main() {
  int n = gi(), a, b, c;
  for (int i = 1; i < n; ++i)
    a = gi(), b = gi(), c = gi(), link(a, b, c), link(b, a, c);
  N = n, f[0] = 1e9, getrt(1);
  solve(rt);
  printf("%lld\n", ans);
  return 0;
}
