#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c;
  long long flag = 1;
  while ((c = getchar()) < '0' || c > '9') {
    if (c == '-') {
      flag = -1;
    }
  }
  long long count = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') {
    count *= 10;
    count += c - '0';
  }
  return count * flag;
}
inline void print(long long x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x > 9) {
    print(x / 10);
  }
  putchar(x % 10 + '0');
}
const long long N = 1e5 + 10;
const long long M = 2e5 + 10;
long long f[N];
long long g[N];
long long siz[N];
long long head[N], net[M], vis[M], wigh[M];
long long tot = 1;
inline void add(long long u, long long v, long long w) {
  tot++;
  net[tot] = head[u];
  head[u] = tot;
  vis[tot] = v;
  wigh[tot] = w;
}
void dfs1(long long u, long long fa) {
  siz[u] = 1;
  for (register long long i = head[u]; i; i = net[i]) {
    long long v = vis[i];
    long long w = wigh[i];
    if (v == fa) {
      continue;
    }
    dfs1(v, u);
    siz[u] += siz[v];
    if (w == 1) {
      f[u] += siz[v];
    } else {
      f[u] += f[v];
    }
  }
}
void dfs2(long long u, long long fa) {
  for (register long long i = head[u]; i; i = net[i]) {
    long long v = vis[i];
    long long w = wigh[i];
    if (v == fa) {
      continue;
    }
    if (w == 1) {
      g[v] = siz[1] - siz[v];
    } else {
      g[v] = g[u] + f[u] - f[v];
    }
    dfs2(v, u);
  }
}
signed main() {
  long long n;
  n = read();
  for (register long long i = 1; i <= n - 1; i++) {
    long long u, v, w;
    u = read();
    v = read();
    w = read();
    long long zzz = 1;
    while (w) {
      if (w % 10 != 7 && w % 10 != 4) {
        zzz = 0;
        break;
      }
      w /= 10;
    }
    add(u, v, zzz);
    add(v, u, zzz);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  long long ans = 0;
  for (register long long i = 1; i <= n; i++) {
    ans += (f[i] * (f[i] - 1) + g[i] * (g[i] - 1) + f[i] * g[i] * 2);
  }
  print(ans);
  return 0;
}
