#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int N = 3e5 + 5;
const int oo = 1e9 + 7;
vector<pair<int, int> > g[N];
vector<pair<int, int> > child[N];
int niche[N][2];
int upore[N][2];
bool vis[N];
int dfs(int u, int p = -1) {
  vis[u] = 1;
  long long sz = 1;
  for (pair<int, int> vw : g[u]) {
    if (vw.first != p and vw.second) sz += dfs(vw.first, u);
  }
  return sz;
}
int dfs2(int u, int p = -1) {
  vis[u] = 1;
  long long sz = 1;
  for (pair<int, int> vw : g[u]) {
    if (vw.first != p and vw.second == 0) sz += dfs2(vw.first, u);
  }
  return sz;
}
void dfs3(int u, int p = -1) {
  for (pair<int, int> vw : g[u])
    if (vw.first != p) {
      int v = vw.first;
      int w = vw.second;
      child[u].push_back(vw);
      dfs3(v, u);
      niche[u][w] += niche[v][w] + 1;
    }
}
void dfs4(int u) {
  if (child[u].size()) {
    int n = child[u].size();
    vector<vector<int> > pre(n, vector<int>(2, 0));
    vector<vector<int> > post(n, vector<int>(2, 0));
    int v = child[u].front().first;
    int w = child[u].front().second;
    pre[0][w] += niche[v][w] + 1;
    v = child[u].back().first;
    w = child[u].back().second;
    post[n - 1][w] += niche[v][w] + 1;
    for (int i = 1; i < n; i++) {
      pre[i][0] += pre[i - 1][0];
      pre[i][1] += pre[i - 1][1];
      pre[i][child[u][i].second] +=
          niche[child[u][i].first][child[u][i].second] + 1;
    }
    for (int i = n - 2; i >= 0; i--) {
      post[i][0] += post[i + 1][0];
      post[i][1] += post[i + 1][1];
      post[i][child[u][i].second] +=
          niche[child[u][i].first][child[u][i].second] + 1;
    }
    for (int i = 0; i < n; i++) {
      pair<int, int> vw = child[u][i];
      int v = vw.first;
      int w = vw.second;
      upore[v][w] += upore[u][w] + 1;
      if (i > 0) upore[v][w] += pre[i - 1][w];
      if (i < n - 1) upore[v][w] += post[i + 1][w];
      dfs4(v);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long sz = dfs(i);
      if (2 <= sz) ans += sz * (sz - 1);
    }
  }
  memset(vis, 0, sizeof vis);
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      long long sz = dfs2(i);
      if (2 <= sz) ans += sz * (sz - 1);
    }
  }
  dfs3(1);
  dfs4(1);
  for (int i = 1; i <= n; i++) {
    long long a = upore[i][0] + niche[i][0];
    long long b = upore[i][1] + niche[i][1];
    ans += a * b;
  }
  cout << ans << "\n";
}
