#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
vector<int> g[MAXN + 1];
bool vis[MAXN + 1];
long long ans;
void fast_io() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
bool lucky(int n) {
  int rem;
  while (n) {
    rem = (n % 10);
    if (rem != 4 and rem != 7) return false;
    n /= 10;
  }
  return true;
}
int dfs(int u) {
  vis[u] = true;
  int sz = 1;
  for (auto v : g[u])
    if (!vis[v]) sz += dfs(v);
  return sz;
}
int main() {
  fast_io();
  int n, u, v, w, x;
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    if (lucky(w)) continue;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  ans = 1LL * n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    x = dfs(i);
    if (x == 1) continue;
    ans -= (1LL * x * (x - 1) * (x - 2));
    ans -= (1LL * x * (x - 1) * (n - x)) << 1;
  }
  cout << ans << '\n';
  return 0;
}
