#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int p[N], s[N];
long long ans = 0;
bool isLucky(int x) {
  while (x) {
    int cur = x % 10;
    if (cur != 4 && cur != 7) return 0;
    x /= 10;
  }
  return 1;
}
int find_set(int x) { return p[x] == x ? x : p[x] = find_set(p[x]); }
void union_sets(int u, int v) {
  u = find_set(u);
  v = find_set(v);
  if (u == v) return;
  p[u] = v;
  s[v] += s[u];
  s[u] = 0;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) p[i] = i, s[i] = 1;
  for (int i = 0; i < n - 1; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (isLucky(w)) continue;
    union_sets(u, v);
  }
  for (int i = 1; i <= n; ++i) {
    int cnt = n - s[find_set(i)];
    ans += (long long)cnt * (cnt - 1);
  }
  printf("%lld", ans);
}
