#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > E[111111];
int P[111111], S[111111];
long long ans;
int find(int x) {
  if (P[x] != x) P[x] = find(P[x]);
  return P[x];
}
bool ok(int x) {
  while (x) {
    int r = x % 10;
    x /= 10;
    if (r != 4 and r != 7) return false;
  }
  return true;
}
void Union(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (S[x] < S[y]) {
    P[x] = y;
    S[y] += S[x];
  } else {
    P[y] = x;
    S[x] += S[y];
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) P[i] = i, S[i] = 1;
  int u, v, c;
  for (int i = 1; i < N; i++) {
    scanf("%d %d %d", &u, &v, &c);
    if (!ok(c)) Union(u, v);
  }
  for (int i = 1; i <= N; i++) {
    long long tmp = N - S[find(i)];
    ans += tmp * (tmp - 1);
  }
  cout << ans << endl;
}
