#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int N, a[MAX], s[MAX];
int ok(int x) {
  char ch[20];
  sprintf(ch, "%d", x);
  for (int i = 0; i < strlen(ch); i++)
    if (ch[i] != '4' && ch[i] != '7') return 1;
  return 0;
}
int findSet(int x) {
  if (x == a[x]) return x;
  return a[x] = findSet(a[x]);
}
void unionSet(int x, int y) {
  int fx = findSet(x);
  int fy = findSet(y);
  a[fx] = fy;
}
int main() {
  while (scanf("%d", &N) == 1) {
    for (int i = 1; i <= N; i++) a[i] = i;
    for (int i = 1; i < N; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      if (ok(w)) unionSet(u, v);
    }
    memset(s, 0, sizeof(s));
    for (int i = 1; i <= N; i++) s[findSet(i)]++;
    long long ans = 0;
    for (int i = 1; i <= N; i++)
      ans += 1LL * s[i] * (N - s[i]) * (N - s[i] - 1);
    printf("%I64d\n", ans);
  }
  return 0;
}
