#include <bits/stdc++.h>
using namespace std;
long long A, P[100005], S[100005], N, k, a, b, c, i;
int q(int n) { return n == P[n] ? n : P[n] = q(P[n]); }
void r(int a, int b) {
  if (q(a) != q(b)) S[q(b)] += S[q(a)], P[q(a)] = q(b);
}
int main() {
  cin >> N;
  for (i = 1; i <= N; i++) S[i] = 1, P[i] = i;
  for (i = 1; i <= N - 1; i++) {
    cin >> a >> b >> c, k = 0;
    while (c) k |= c % 10 != 4 && c % 10 != 7, c /= 10;
    if (k) r(a, b);
  }
  for (i = 1; i <= N; i++) A += (N - S[q(i)]) * (N - S[q(i)] - 1);
  cout << A;
}
