#include <bits/stdc++.h>
using namespace std;
long long n, w[500000], a, b, c;
long long sz[500000];
long long ans, temp;
long long gett(long long x) {
  if (x == w[x]) return x;
  return w[x] = gett(w[x]);
}
void merg(long long a, long long b) {
  a = gett(a);
  b = gett(b);
  if (rand() % 2) swap(a, b);
  w[a] = b;
  sz[b] += sz[a];
}
bool luck(long long x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) w[i] = i, sz[i] = 1;
  for (int i = 1; i < n; i++) {
    cin >> a >> b >> c;
    if (luck(c) == 0) merg(a, b);
  }
  ans = n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    if (w[i] != i) continue;
    temp = sz[i];
    ans -= temp * (temp - 1) * (temp - 2);
    ans -= temp * (n - temp) * (temp - 1) * 2;
  }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}
