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>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T>
using Table = vector<vector<T>>;
const ld eps = 1e-9;
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "( " << v.first << ", " << v.second << ")";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<set<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const map<T, T> &v) {
  int i = 0;
  for (auto it : v) {
    if (i > 0) {
      os << ' ';
    }
    os << it.first << ' ' << it.second;
    i++;
  }
  return os;
}
using ll = long long;
struct Edge {
  int u;
  int v;
  ll cost;
};
int N, M, Q;
using Graph = vector<vector<Edge>>;
Graph g;
struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  g.resize(N);
  UnionFind uf1(N), uf2(N);
  for (int i = 0; i < N - 1; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    g[u].push_back(Edge{u, v, c});
    g[v].push_back(Edge{v, u, c});
    if (c) {
      uf1.unionSet(u, v);
    } else {
      uf2.unionSet(u, v);
    }
  }
  ll answer = 0;
  for (int i = 0; i < N; ++i) {
    {
      { answer += (uf2.size(i) - 1) * ll(uf1.size(i) - 1); }
    }
  }
  set<int> used;
  {
    for (int i = 0; i < N; ++i) {
      if (used.find(uf1.root(i)) == used.end()) {
        used.emplace(uf1.root(i));
        answer += uf1.size(i) * ll(uf1.size(i) - 1);
      }
    }
    used.clear();
    for (int i = 0; i < N; ++i) {
      if (used.find(uf2.root(i)) == used.end()) {
        used.emplace(uf2.root(i));
        answer += uf2.size(i) * ll(uf2.size(i) - 1);
      }
    }
  }
  cout << answer << endl;
  return 0;
}
