#include <bits/stdc++.h>
int siz[100005];
int g[100005];
int FIND(int x) { return g[x] == x ? g[x] : g[x] = FIND(g[x]); }
void UNION(int x, int y) {
  siz[FIND(y)] += siz[FIND(x)];
  g[FIND(x)] = FIND(y);
}
long long ans = 0;
bool islucky(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main(void) {
  int n, x, y, z;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) g[i] = i, siz[i] = 1;
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    if (islucky(z)) continue;
    UNION(x, y);
  }
  ans = 1LL * n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    if (g[i] == i) {
      ans -= 2LL * siz[i] * (siz[i] - 1) * (n - 2);
      ans += 1LL * siz[i] * (siz[i] - 1) * (siz[i] - 2);
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
