#include <bits/stdc++.h>
using namespace std;
int n, tot, head[100001], w[100001], a[100001];
struct data {
  int next, num, w;
} edge[200001];
void adde(int u, int v, int w) {
  edge[++tot].next = head[u];
  edge[tot].num = v;
  edge[tot].w = w;
  head[u] = tot;
}
void add(int u, int v, int w) {
  adde(u, v, w);
  adde(v, u, w);
}
bool check(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
void dfs1(int x, int fa) {
  w[x] = 1;
  for (int i = head[x]; i != -1; i = edge[i].next) {
    int kx = edge[i].num;
    if (kx == fa) continue;
    dfs1(kx, x);
    w[x] += w[kx];
    if (edge[i].w)
      a[x] += w[kx];
    else
      a[x] += a[kx];
  }
}
void dfs2(int x, int fa, int sum) {
  for (int i = head[x]; i != -1; i = edge[i].next) {
    int kx = edge[i].num;
    if (kx == fa) continue;
    if (edge[i].w)
      dfs2(kx, x, n - w[kx]);
    else
      dfs2(kx, x, sum + a[x] - a[kx]);
  }
  a[x] += sum;
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (check(w))
      add(u, v, 1);
    else
      add(u, v, 0);
  }
  dfs1(1, -1);
  dfs2(1, -1, 0);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long now = a[i];
    now *= (now - 1);
    ans += now;
  }
  printf("%lld\n", ans);
  return 0;
}
