#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int n, f[MAXN], g[MAXN], sz[MAXN], sf[MAXN] = {};
vector<pair<int, bool> > adj[MAXN];
void dfs_f(int u, int p) {
  sz[u] = 1;
  for (auto it : adj[u]) {
    int v = it.first;
    if (v == p) continue;
    bool ok = it.second;
    dfs_f(v, u);
    f[u] += (ok ? sz[v] : f[v]), sz[u] += sz[v];
  }
}
void dfs_g(int u, int p, bool ok) {
  if (ok)
    g[u] = n - sz[u];
  else if (u > 1)
    g[u] = g[p] + sf[p] - f[u];
  for (auto it : adj[u]) {
    int v = it.first;
    if (v == p) continue;
    bool ok = it.second;
    sf[u] += (ok ? sz[v] : f[v]);
  }
  for (auto it : adj[u]) {
    int v = it.first;
    if (v == p) continue;
    bool ok = it.second;
    dfs_g(v, u, ok);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; ++i) {
    bool ok = 1;
    int u, v, c;
    cin >> u >> v >> c;
    while (c) ok &= (c % 10 == 4 || c % 10 == 7), c /= 10;
    adj[u].push_back({v, ok}), adj[v].push_back({u, ok});
  }
  dfs_f(1, 0), dfs_g(1, 0, 0);
  long long res = 0;
  for (int i = 1; i <= n; ++i) {
    int tot = g[i] + f[i];
    res += 1LL * tot * (tot - 1);
  }
  cout << res;
  return 0;
}
