#include <bits/stdc++.h>
using namespace std;
int n, fa[100000 + 5], siz[100000 + 5];
int xfind(int x) { return (fa[x] == x) ? x : fa[x] = xfind(fa[x]); }
bool check(int num) {
  while (num) {
    int p = num % 10;
    if (p != 4 && p != 7) return 0;
    num /= 10;
  }
  return 1;
}
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)) {
      int a = xfind(u), b = xfind(v);
      fa[a] = b;
      siz[b] += siz[a];
    }
  }
  long long ans = 1LL * n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++)
    if (xfind(i) == i) {
      if (siz[i] >= 2) ans -= 1LL * siz[i] * (siz[i] - 1) * (siz[i] - 2);
      ans -= 2LL * siz[i] * (siz[i] - 1) * (n - siz[i]);
    }
  printf("%lld", ans);
}
