#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int head[200010], nxt[400010], end[400010], value[400010];
int cou = 0;
long long f1[200010], f2[200010], f3[200010], f4[200010];
long long ans = 0;
void link(int a, int b, int v) {
  nxt[++cou] = head[a];
  head[a] = cou;
  end[cou] = b;
  value[cou] = v;
}
void dfs(int cur, int last) {
  for (int x = head[cur]; x != -1; x = nxt[x]) {
    if (end[x] == last) continue;
    dfs(end[x], cur);
    if (value[x])
      f1[cur] += f1[end[x]] + 1;
    else
      f2[cur] += f2[end[x]] + 1;
  }
}
void dfs2(int cur, int last) {
  for (int x = head[cur]; x != -1; x = nxt[x]) {
    if (end[x] != last) {
      if (value[x])
        f1[end[x]] = f1[cur];
      else
        f2[end[x]] = f2[cur];
      dfs2(end[x], cur);
    }
  }
  ans += f1[cur] * f2[cur] + f1[cur] + f2[cur];
}
int main() {
  memset(head, -1, sizeof(head));
  int n = inp();
  for (int i = 1; i < n; i++) {
    int u = inp();
    int v = inp();
    int w = inp();
    link(u, v, w);
    link(v, u, w);
  }
  dfs(1, 0);
  dfs2(1, 0);
  std::cout << ans << std::endl;
}
