#include <bits/stdc++.h>
#pragma comment(linker, "/STACK: 2000000")
using namespace std;
int main(void) {
  srand(time(0));
  cout << fixed << setprecision(7);
  cerr << fixed << setprecision(7);
  int n;
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  function<int(int)> ok = [&](int k) {
    return !k ? 0 : ok(k / 10) | ((k % 10 != 4) && (k % 10 != 7));
  };
  static int f[1 << 20];
  for (int i = 1; i <= n; ++i) f[i] = i;
  function<int(int)> get = [&](int k) {
    return k == f[k] ? k : f[k] = get(f[k]);
  };
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (ok(c)) f[get(a)] = get(b);
  }
  static int S[1 << 20];
  for (int i = 1; i <= n; ++i) ++S[get(i)];
  long long ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += 1ll * (n - S[get(i)]) * (n - S[get(i)] - 1);
  cout << ans << '\n';
  return 0;
}
