#include <bits/stdc++.h>
using namespace std;
inline long long get_num(char ch) {
  if (ch == '-')
    return 0;
  else if (ch >= 'a' && ch <= 'z') {
    return 1 + (ch - 'a');
  } else if (ch >= 'A' && ch <= 'Z') {
    return 27 + (ch - 'A');
  } else if (ch >= '0' && ch <= '9') {
    return 53 + (ch - '0');
  }
}
inline int GA(long long n, vector<long long>& vec) {
  for (long long i = 0; i < n; ++i) {
    long long x;
    cin >> x;
    vec.push_back(x);
  }
}
inline int GAEG(long long m, vector<long long> vec[]) {
  for (long long i = 0; i < m; ++i) {
    long long x, y;
    cin >> x >> y;
    x--, y--;
    vec[x].push_back(y);
    vec[y].push_back(x);
  }
}
long long first(long long x) {
  while (x) {
    if (x % 10 != 7 && x % 10 != 4) return true;
    x /= 10;
  }
  return false;
}
long long ft[100001];
long long sz[100001];
long long ff(long long x) { return (ft[x] < 0) ? x : ft[x] = ff(ft[x]); }
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; ++i) sz[i] = 1;
  memset(ft, -1, sizeof(ft));
  for (long long i = 0; i < n - 1; ++i) {
    long long x, y, z;
    cin >> x >> y >> z;
    x--, y--;
    if (first(z)) {
      long long fix = ff(x), fiy = ff(y);
      ft[fiy] = fix, sz[fix] += sz[fiy];
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    long long pp = ff(i);
    long long qq;
    if (pp != -1)
      qq = n - sz[ff(i)];
    else
      qq = n - 1;
    ans += qq * (qq - 1);
  }
  cout << ans << endl;
}
