#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct edge {
  int v, w, nxt;
} g[N * 2];
int head[N], sz = 0;
void add(int u, int v, int w) {
  g[++sz] = {v, w, head[u]};
  head[u] = sz;
}
bool check(int w) {
  for (; w; w /= 10)
    if (w % 10 != 7 && w % 10 != 4) return false;
  return true;
}
int n;
int f[N], siz[N];
void dfs(int u, int p) {
  f[u] = 0;
  siz[u] = 1;
  for (int i = head[u], v; i; i = g[i].nxt)
    if ((v = g[i].v) != p) {
      dfs(v, u);
      siz[u] += siz[v];
      f[u] += g[i].w ? siz[v] : f[v];
    }
}
long long ans = 0;
void dfs2(int u, int p, int cnt) {
  long long tot = cnt + f[u];
  ans += tot * (tot - 1);
  for (int i = head[u], v; i; i = g[i].nxt)
    if ((v = g[i].v) != p) {
      int cnt2 = g[i].w ? n - siz[v] : f[u] + cnt - f[v];
      dfs2(v, u, cnt2);
    }
}
int main() {
  scanf("%d", &n);
  for (long long i = (1); i <= (n - 1); i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    w = check(w);
    add(u, v, w);
    add(v, u, w);
  }
  dfs(1, 0);
  dfs2(1, 0, 0);
  cout << ans;
}
