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 Day12[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int prime100[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37,  41, 43,
                  47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
template <typename T>
inline bool isLeap(T y) {
  return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false);
}
template <typename T>
inline T GCD(T a, T b) {
  a = abs(a);
  b = abs(b);
  if (a < b) swap(a, b);
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
template <typename T>
inline T LCM(T x, T y) {
  T tp = GCD(x, y);
  if ((x / tp) * 1. * y > 9e18) return 9e18;
  return (x / tp) * y;
}
template <typename T>
inline T BIGMOD(T A, T B, T M = 1000000007) {
  T ret = 1;
  while (B) {
    if (B & 1) ret = (ret * A) % M;
    A = (A * A) % M;
    B = B >> 1;
  }
  return ret;
}
template <typename T>
inline T BigMod(T A, T B, T M) {
  T ret = 1;
  while (B) {
    if (B & 1) ret = (ret * A) % M;
    A = (A * A) % M;
    B = B >> 1;
  }
  return ret;
}
long long int MySqrt(long long int n) {
  long long int p = sqrt(n);
  if ((p + 1) * (p + 1) <= n)
    return p + 1;
  else if (p * p <= n)
    return p;
  else
    return p - 1;
}
long long int MyPow(long long int x, long long int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return MyPow(x * x, n / 2);
  else
    return x * MyPow(x * x, ((n - 1) / 2));
}
long long int modInverse(long long int n) {
  return BIGMOD(n, (long long int)1000000007 - 2) % 1000000007;
}
long long int dp[3][200005];
int Par[3][200005];
void init(int n) {
  for (int i = 1; i <= n; i++) {
    dp[0][i] = 1;
    dp[1][i] = 1;
    Par[0][i] = i;
    Par[1][i] = i;
  }
}
int Find_Par(int a, int c) {
  if (Par[c][a] == a) return a;
  Par[c][a] = Find_Par(Par[c][a], c);
  return Par[c][a];
}
void Make(int x, int y, int c) {
  int a = Find_Par(x, c);
  int b = Find_Par(y, c);
  if (dp[c][a] > dp[c][b]) {
    dp[c][a] += dp[c][b];
    Par[c][b] = a;
  } else {
    dp[c][b] += dp[c][a];
    Par[c][a] = b;
  }
}
int main() {
  int n;
  scanf("%d", &(n));
  init(n);
  for (int i = 1; i < n; i++) {
    int x, y, c;
    scanf("%d%d%d", &(x), &(y), &(c));
    Make(x, y, c);
  }
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (Par[0][i] == i) ans += dp[0][i] * (dp[0][i] - 1);
    if (Par[1][i] == i) ans += dp[1][i] * (dp[1][i] - 1);
    int a = Find_Par(i, 0);
    int b = Find_Par(i, 1);
    ans += (dp[0][a] - 1) * (dp[1][b] - 1);
  }
  printf("%lld\n", ans);
}
