#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O8")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long int N = 1e5 + 20, mod = 1e9 + 7, inf = 2e9, maxq = 1e5 + 30;
const long double eps = 0.0000001;
inline bool lucky(int w) {
  while (w != 0) {
    if (w % 10 != 4 && w % 10 != 7) return 0;
    w /= 10;
  }
  return 1;
}
vector<int> comp[N];
int c[N], par[N];
int getpar(int x) {
  if (par[x] == x) return x;
  return par[x] = getpar(par[x]);
}
void merge(int x, int y) {
  if (getpar(x) == getpar(y)) return;
  if (comp[par[x]].size() < comp[par[y]].size()) swap(x, y);
  int sz = comp[par[y]].size();
  while (sz) {
    comp[par[x]].push_back(comp[par[y]][sz - 1]);
    sz--;
    comp[par[y]].pop_back();
  }
  par[par[y]] = par[x];
  return;
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i < n + 1; i++) {
    par[i] = i;
    comp[i].push_back(i);
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    if (!lucky(w)) merge(x, y);
  }
  long long int ans = 0;
  for (int i = 1; i < n + 1; i++) {
    ans +=
        1ll * comp[i].size() * (n - comp[i].size()) * (n - comp[i].size() - 1);
  }
  cout << ans;
  return 0;
}
