#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long power(long long a, long long n) {
  a %= 1000000007;
  if (n == 1) return a;
  if (n == 0) return 1;
  if (n % 2)
    return (a * (power((a * a) % 1000000007, n / 2) % 1000000007)) % 1000000007;
  return power((a * a) % 1000000007, n / 2) % 1000000007;
}
const long long inf = (long long)1e18;
long long inverse(long long x) { return power(x, 1000000007 - 2) % 1000000007; }
long long n, ans = 0;
vector<pair<long long, long long> > adj[200005];
vector<long long> zc(200005), oc(200005), cnt(200005), par(200005),
    eval(200005);
set<long long> lvs;
void dfs(long long x) {
  for (auto &y : adj[x]) {
    if (par[x] != y.first) {
      par[y.first] = x;
      eval[y.first] = y.second;
      dfs(y.first);
    }
  }
}
void prune(long long root) {
  while ((long long)lvs.size()) {
    long long x = (*lvs.begin());
    lvs.erase(lvs.begin());
    zc[x] = oc[x] = 1;
    for (auto &y : adj[x]) {
      if (y.first != par[x]) {
        if (y.second == 1)
          oc[x] += oc[y.first];
        else
          zc[x] += zc[y.first] + oc[y.first] - 1;
      }
    }
    if (x == root) break;
    if (x != root) cnt[par[x]]++;
    if (par[x] != root && cnt[par[x]] == (long long)adj[par[x]].size() - 1 ||
        par[x] == root && cnt[root] == (long long)adj[root].size()) {
      lvs.insert(par[x]);
    }
  }
}
void dfs2(long long x) {
  if (x != 1) {
    if (eval[x] == 1)
      oc[x] += oc[par[x]] - oc[x];
    else
      zc[x] += zc[par[x]] + oc[par[x]] - zc[x] - oc[x];
  }
  ans += oc[x] + zc[x] - 2;
  for (auto &y : adj[x]) {
    if (y.first != par[x]) {
      dfs2(y.first);
    }
  }
}
void solve() {
  cin >> n;
  for (long long i = 1; i < n; ++i) {
    long long u, v, x;
    cin >> u >> v >> x;
    adj[u].push_back({v, x});
    adj[v].push_back({u, x});
  }
  par[1] = 1;
  dfs(1);
  for (long long i = 2; i < n + 1; ++i)
    if ((long long)adj[i].size() == 1) lvs.insert(i);
  prune(1);
  dfs2(1);
  cout << ans << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long T = 1;
  for (long long t = 1; t < T + 1; ++t) {
    solve();
  }
  return 0;
}
