#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, u, v, w, res = 0, par[N];
vector<pair<long long, long long> > a[N];
long long root(long long x) {
  if (par[x] < 0) return x;
  return (par[x] = root(par[x]));
}
void Merge(long long u, long long v) {
  long long a = root(u), b = root(v);
  if (a == b) return;
  if (par[a] > par[b]) swap(a, b);
  par[a] += par[b], par[b] = a;
}
bool check(long long n) {
  while (n > 0) {
    long long a = n % 10;
    if (a != 4 && a != 7) return false;
    n /= 10;
  }
  return true;
}
void in() {
  cin >> n;
  for (long long i = 1; i <= n; i++) par[i] = -1;
  for (long long i = 1; i <= n - 1; i++) {
    cin >> u >> v >> w;
    if (!check(w)) Merge(u, v);
    a[u].push_back({v, w}), a[v].push_back({u, w});
  }
}
void process() {
  for (long long i = 1; i <= n; i++) {
    if (par[i] < 0) {
      long long tmp = -par[i];
      res += (tmp * (n - tmp - 1) * (n - tmp));
    }
  }
  cout << res;
}
int main() {
  if (fopen("trau.inp", "r"))
    freopen("trau.inp", "r", stdin), freopen("trau.out", "w", stdout);
  in();
  process();
}
