#include <bits/stdc++.h>
using namespace std;
long long n;
const long long N = 1e5 + 7;
vector<pair<long long, long long>> g[N];
bool is_happy(long long x) {
  bool ok = true;
  while (x) {
    long long d = x % 10;
    ok &= (d == 4 || d == 7);
    x /= 10;
  }
  return ok;
}
long long F[N], F1[N], d[N];
void calc(long long v, long long p = -1) {
  d[v] = 1;
  for (auto u : g[v]) {
    if (u.first == p) continue;
    calc(u.first, v);
    d[v] += d[u.first];
  }
}
void dfs(long long v, long long p = -1) {
  for (auto u : g[v]) {
    if (u.first == p) continue;
    dfs(u.first, v);
    if (is_happy(u.second))
      F[v] += d[u.first];
    else
      F[v] += F[u.first];
  }
}
void dfs1(long long v, long long p = -1) {
  for (auto u : g[v]) {
    if (u.first == p) continue;
    if (is_happy(u.second))
      F1[u.first] = d[1] - d[u.first];
    else
      F1[u.first] = F1[v] + F[v] - F[u.first];
    dfs1(u.first, v);
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n - 1; ++i) {
    long long u, v, c;
    cin >> u >> v >> c;
    g[u].push_back({v, c});
    g[v].push_back({u, c});
  }
  calc(1);
  dfs(1);
  dfs1(1);
  long long ans = 0;
  for (long long i = 1; i <= n; ++i)
    ans += F[i] * (F[i] - 1) + F1[i] * (F1[i] - 1) + F[i] * F1[i] * 2;
  cout << ans;
}
