#include <bits/stdc++.h>
int read() {
  static int ch, x;
  while (isspace(ch = getchar())) {
  }
  x = ch ^ 48;
  while (isdigit(ch = getchar())) x = (((x << 2) + x) << 1) + (ch ^ 48);
  return x;
}
int readchk() {
  static int ch;
  static bool flag;
  while (isspace(ch = getchar())) {
  }
  flag = ch == 52 || ch == 55;
  while (isdigit(ch = getchar())) flag = flag && (ch == 52 || ch == 55);
  return flag;
}
struct edge {
  int next, to;
} e[100010 << 1];
int ei;
long long ans;
int x[100010], y[100010], fa[100010], head[100010], cnt[100010], siz[100010];
int findf(const int &x) { return fa[x] ? (fa[x] = findf(fa[x])) : x; }
void dfs(const int &f, const int &fa, const int &n) {
  ans += 1ll * siz[f] * (n - siz[f]) * (n - siz[f] - 1);
  for (int i = head[f]; i; i = e[i].next) {
    const int t = e[i].to;
    if (t != fa) dfs(t, f, n);
  }
}
int main() {
  const int n = read();
  for (int i = 1; i <= n; ++i) siz[i] = 1;
  for (int i = n; --i;) {
    x[i] = read();
    y[i] = read();
    if (!readchk()) {
      const int fx = findf(x[i]), fy = findf(y[i]);
      if (fx != fy) {
        fa[fx] = fy;
        siz[fy] += siz[fx];
      }
    }
  }
  for (int i = n; --i;) {
    const int fx = findf(x[i]), fy = findf(y[i]);
    if (fx != fy) {
      (e[++ei].next = head[fx], e[ei].to = (fy), head[fx] = ei);
      (e[++ei].next = head[fy], e[ei].to = (fx), head[fy] = ei);
    }
  }
  dfs(findf(1), 0, n);
  printf("%lld", ans);
  return 0;
}
