#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmin(T& a, const T& b) {
  return a > b ? a = b, true : false;
}
template <class T>
bool chmax(T& a, const T& b) {
  return a < b ? a = b, true : false;
}
const long long mod = 1e9 + 7, N = 2e5 + 5;
inline long long msb(long long val) {
  return sizeof(long long) * 8 - __builtin_clzll(val) - 1;
}
long long a[N], n, m, k;
vector<pair<long long, long long> > edges[N];
bool check(long long a) {
  while (a > 0) {
    if (a % 10 != 4 && a % 10 != 7) return false;
    a /= 10;
  }
  return true;
}
struct des {
  vector<long long> p, sz;
  void init() {
    p.assign(n + 1, 0);
    sz.assign(n + 1, 1);
    for (long long i = 0; i < n; i++) p[i] = i;
  }
  long long par(long long u) {
    if (p[u] == u)
      return u;
    else
      return p[u] = par(p[u]);
  }
  bool issame(long long u, long long v) { return (par(u) == par(v)); }
  void merge(long long u, long long v) {
    u = par(u);
    v = par(v);
    if (u == v) return;
    p[v] = u;
    sz[u] += sz[v];
  }
  long long get_size(long long u) { return sz[par(u)]; }
} d;
void solve(long long test_case) {
  long long i, j;
  cin >> n;
  d.init();
  for (i = 1; i < n; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (!check(w)) d.merge(u, v);
  }
  long long ans = 0;
  for (i = 1; i <= n; i++) {
    long long t = n - d.get_size(i);
    ans += t * (t - 1);
  }
  cout << ans << '\n';
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  solve(1);
  return 0;
}
