#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T>
void write(T x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  puts("");
}
long long n, ans;
vector<pair<long long, long long>> zero, one;
long long fa[200005];
long long val0[200005], val1[200005];
vector<long long> mem[200005];
long long fnd(long long x) {
  if (fa[x] != x) fa[x] = fnd(fa[x]);
  return fa[x];
}
signed main() {
  read(n);
  for (long long i = 1; i < n; ++i) {
    long long x, y, c;
    read(x);
    read(y);
    read(c);
    if (c == 0)
      zero.push_back(make_pair(x, y));
    else
      one.push_back(make_pair(x, y));
  }
  for (long long i = 1; i <= n; ++i) fa[i] = i;
  for (auto i : zero) {
    long long fx = fnd(i.first), fy = fnd(i.second);
    if (fx != fy) fa[fx] = fy;
  }
  for (long long i = 1; i <= n; ++i) mem[fnd(i)].push_back(i);
  for (long long i = 1; i <= n; ++i)
    if (fnd(i) == i) {
      ans += mem[i].size() * ((signed)mem[i].size() - 1);
      for (long long j : mem[i]) val0[j] = (signed)mem[i].size() - 1;
    }
  for (long long i = 1; i <= n; ++i) {
    fa[i] = i;
    mem[i].clear();
  }
  for (auto i : one) {
    long long fx = fnd(i.first), fy = fnd(i.second);
    if (fx != fy) fa[fx] = fy;
  }
  for (long long i = 1; i <= n; ++i) mem[fnd(i)].push_back(i);
  for (long long i = 1; i <= n; ++i)
    if (fnd(i) == i) {
      ans += mem[i].size() * ((signed)mem[i].size() - 1);
      for (long long j : mem[i]) val1[j] = (signed)mem[i].size() - 1;
    }
  for (long long i = 1; i <= n; ++i) ans += val0[i] * val1[i];
  cout << ans << endl;
  return 0;
}
