#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > mv;
int val[200001][3];
long long res;
void fi(int ac, int fa) {
  int i, s = mv[ac].size();
  val[ac][0] = val[ac][1] = val[ac][2] = 0;
  for (i = 0; i < s; i++)
    if (mv[ac][i].first != fa) {
      fi(mv[ac][i].first, ac);
      if (mv[ac][i].second) {
        val[ac][1] += val[mv[ac][i].first][1] + 1;
        val[ac][2] += val[mv[ac][i].first][2] + val[mv[ac][i].first][0];
      } else {
        val[ac][0] += val[mv[ac][i].first][0] + 1;
      }
    }
}
void se(int ac, int fa, int ed) {
  if (ed) {
    val[ac][1] = val[fa][1];
    val[ac][2] = val[fa][2] - val[ac][0] + val[fa][0];
  } else
    val[ac][0] = val[fa][0];
  res += val[ac][0] + val[ac][1] + val[ac][2];
  int i, s = mv[ac].size();
  for (i = 0; i < s; i++)
    if (mv[ac][i].first != fa) {
      se(mv[ac][i].first, ac, mv[ac][i].second);
    }
}
int main() {
  int n;
  scanf("%d", &n);
  mv.resize(n);
  int i, u, v, c;
  for (i = 0; i < n - 1; i++) {
    scanf("%d %d %d", &u, &v, &c);
    mv[u - 1].push_back(pair<int, int>(v - 1, c));
    mv[v - 1].push_back(pair<int, int>(u - 1, c));
  }
  fi(0, -1);
  res = val[0][0] + val[0][1] + val[0][2];
  int s = mv[0].size();
  for (i = 0; i < s; i++) se(mv[0][i].first, 0, mv[0][i].second);
  printf("%lld\n", res);
  return 0;
}
