#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5;
vector<long long> forest[MX][2];
long long cnt[MX][2];
bool vis[MX][2];
vector<long long> nodes;
void dfs(long long u, long long w) {
  nodes.push_back(u);
  vis[u][w] = true;
  for (long long v : forest[u][w]) {
    if (!vis[v][w]) {
      dfs(v, w);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < (long long)n - 1; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    forest[u][w].push_back(v);
    forest[v][w].push_back(u);
  }
  long long ans = 0;
  long long full = 0;
  for (int u = 0; u < n; u++) {
    for (long long b = 0; b <= 1; b++) {
      if (!vis[u][b]) {
        nodes.clear();
        dfs(u, b);
        for (long long v : nodes) {
          cnt[v][b] = (long long)nodes.size() - 1;
        }
      }
    }
    full += cnt[u][0] + cnt[u][1];
    ans += cnt[u][0] * cnt[u][1];
  }
  ans += full;
  cout << ans << "\n";
  return 0;
}
