#include <bits/stdc++.h>
using namespace std;
vector<pair<int, bool>> v[200001];
int jos[200001][2], sus[200001][2];
long long rasp;
void dfs1(int r, int t) {
  int i, rv;
  for (i = 0; i < v[r].size(); i++)
    if (v[r][i].first != t) {
      rv = v[r][i].first;
      dfs1(rv, r);
      if (v[r][i].second == 0)
        jos[r][0] = jos[r][0] + jos[rv][0] + 1;
      else
        jos[r][1] = jos[r][1] + jos[rv][1] + 1;
    }
}
void dfs2(int r, int t) {
  int i, rv;
  for (i = 0; i < v[r].size(); i++)
    if (v[r][i].first != t) {
      rv = v[r][i].first;
      sus[rv][v[r][i].second] = sus[r][v[r][i].second] +
                                jos[r][v[r][i].second] -
                                jos[rv][v[r][i].second];
      rasp = rasp + 1ll * (jos[rv][v[r][i].second] + 1) *
                        (jos[r][v[r][i].second] - jos[rv][v[r][i].second] - 1);
      dfs2(rv, r);
    }
  rasp = rasp + 1ll * sus[r][0] * jos[r][1] + 1ll * sus[r][1] * jos[r][0] +
         1ll * jos[r][0] * jos[r][1];
  rasp = rasp + 2ll * jos[r][0] + 2ll * jos[r][1];
}
int main() {
  int i, n, x, y;
  bool z;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  dfs1(1, 0);
  dfs2(1, 0);
  cout << rasp;
  return 0;
}
