#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  return x * f;
}
long long sz[100010];
bool ed[200010];
long long cnt, hd[200010];
long long dp[100010], qb[100010];
struct edge {
  long long v, w, nx;
} e[200010];
long long n;
bool is(long long k) {
  bool fl = 0;
  while (k) {
    if (k % 10 != 4 && k % 10 != 7) {
      fl = 1;
      break;
    }
    k /= 10;
  }
  if (fl) return 0;
  return 1;
}
void add(long long u, long long v, long long w) {
  e[++cnt] = {v, w, hd[u]};
  hd[u] = cnt;
  ed[cnt] = is(w);
}
void dfs(long long u, long long fat) {
  sz[u] = 1;
  for (int i = hd[u]; i; i = e[i].nx)
    if (e[i].v != fat) dfs(e[i].v, u), sz[u] += sz[e[i].v];
}
void Dfs(long long u, long long fat) {
  for (int i = hd[u]; i; i = e[i].nx) {
    if (e[i].v != fat) {
      long long v = e[i].v;
      Dfs(v, u);
      if (ed[i])
        dp[u] += sz[v];
      else
        dp[u] += dp[v];
    }
  }
}
void DFS(long long u, long long fat) {
  for (int i = hd[u]; i; i = e[i].nx) {
    long long v = e[i].v;
    if (e[i].v == fat) {
      if (ed[i])
        qb[u] = n - sz[u];
      else
        qb[u] = qb[fat] + dp[fat] - dp[u];
    }
  }
  for (int i = hd[u]; i; i = e[i].nx) {
    long long v = e[i].v;
    if (e[i].v != fat) DFS(v, u);
  }
}
long long ans;
int main() {
  n = rd();
  for (int i = 1; i <= n - 1; i++) {
    long long u = rd(), v = rd(), w = rd();
    add(u, v, w), add(v, u, w);
  }
  dfs(1, 0);
  Dfs(1, 0);
  for (int i = 1; i <= 1; i++) DFS(1, 0);
  for (int i = 1; i <= n; i++) {
    if (dp[i]) ans += dp[i] * (dp[i] - 1);
    if (qb[i]) ans += qb[i] * (qb[i] - 1);
    ans += dp[i] * qb[i] * 2;
  }
  printf("%lld", ans);
  return 0;
}
