#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 2e5 + 5;
bool check(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int fa[N], sz[N];
int find_father(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find_father(fa[x]);
}
void merge(int x, int y) {
  int f1 = find_father(x), f2 = find_father(y);
  if (f1 == f2) return;
  fa[f2] = f1;
  sz[f1] += sz[f2];
}
int main() {
  int n;
  scanf("%d", &n);
  int m = n - 1;
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    sz[i] = 1;
  }
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (!check(w)) {
      merge(u, v);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long f = n - sz[find_father(i)];
    ans += f * (f - 1);
  }
  printf("%I64d\n", ans);
}
