#include <bits/stdc++.h>
int n, fa[100005], siz[100005];
bool check(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (check(w)) continue;
    int f1 = find(u), f2 = find(v);
    fa[f2] = f1, siz[f1] += siz[f2];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int t = n - siz[find(i)];
    ans += 1ll * t * (t - 1);
  }
  printf("%I64d\n", ans);
}
