#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 100;
long long arr[N], cnt, mark[N];
vector<long long> g[N];
void dfs(long long v) {
  mark[v] = 1;
  for (long long u : g[v])
    if (!mark[u]) {
      cnt++;
      dfs(u);
    }
  return;
}
bool check(long long n) {
  long long t;
  while (n != 0) {
    t = n % 10;
    n /= 10;
    if (t != 4 && t != 7) return 0;
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, ans = 0, k, u, v, w;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> u >> v >> w;
    if (!check(w)) {
      g[v].push_back(u);
      g[u].push_back(v);
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (mark[i] == 0) {
      cnt = 1;
      dfs(i);
      k = n - cnt;
      ans += (k * (k - 1) * cnt);
    }
  }
  cout << ans;
  return 0;
}
