#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9;
bool lucky(long long x) {
  while (x) {
    long long d = x % 10;
    if (d != 4 and d != 7) return 0;
    x /= 10;
  }
  return 1;
}
vector<long long> adj[N];
bool vis[N];
long long cnt = 0;
void dfs(long long node) {
  vis[node] = 1;
  cnt++;
  for (long long u : adj[node])
    if (!vis[u]) dfs(u);
}
int32_t main() {
  long long n;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (!lucky(w)) {
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    cnt = 0;
    if (!vis[i]) dfs(i);
    long long d = n - cnt;
    ans += d * (d - 1) * cnt;
  }
  cout << ans << "\n";
}
