#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
int n, par[N], s[N];
long long ans;
int find_par(int v) { return v == par[v] ? v : par[v] = find_par(par[v]); }
bool unlucky(int m) {
  while (m) {
    if (m % 10 != 4 && m % 10 != 7) return true;
    m /= 10;
  }
  return false;
}
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) par[i] = i;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    if (unlucky(w)) par[find_par(u)] = find_par(v);
  }
  for (int i = 0; i < n; i++) s[find_par(i)]++;
  for (int i = 0; i < n; i++)
    ans += (long long)s[i] * (n - s[i]) * (n - s[i] - 1);
  cout << ans << '\n';
  return 0;
}
