#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(const _Tp &a,
                                                     const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(const _Tp &a,
                                                     const _Tp &b) {
  (a > b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
  register char ch(getchar());
  bool f(false);
  while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (ch >= 48 && ch <= 57)
    x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  if (f) x = -x;
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
const long long N = 1e5 + 10;
vector<pair<long long, long long>> g[N];
long long n, sz[N], dp1[N], dp2[N], par[N], lucky_above[N];
bool lucky(long long x) {
  for (; x; x /= 10)
    if ((x % 10) != 4 and (x % 10) != 7) return 0;
  return 1;
}
void dfs1(long long u, long long p) {
  sz[u] = 1;
  par[u] = p;
  for (auto [v, w] : g[u]) {
    if (v == p) continue;
    dfs1(v, u);
    sz[u] += sz[v];
    if (w) {
      lucky_above[v] = 1;
      dp1[u] += sz[v];
    } else
      dp1[u] += dp1[v];
  }
}
void dfs2(long long u, long long p) {
  if (u != 1) {
    if (lucky_above[u]) {
      dp2[u] = n - sz[u];
    } else {
      dp2[u] = dp2[par[u]] + dp1[par[u]] - dp1[u];
    }
  }
  for (auto [v, w] : g[u]) {
    if (v == p) continue;
    dfs2(v, u);
  }
}
int32_t main() {
  read(n);
  long long u, v, w;
  for (long long i = 1; i < n; i++) {
    read(u, v, w);
    w = lucky(w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs1(1, 0);
  dfs2(1, 0);
  long long ans = 0, tot = 0;
  for (long long i = 1; i <= n; i++) {
    tot = dp1[i] + dp2[i];
    ans += (tot * (tot - 1));
  }
  cout << ans << "\n";
}
