#include <bits/stdc++.h>
using namespace std;
int up[220000], down[220000];
int cost[220000], cd[222222], cu[222222];
struct edge {
  int v, w, next;
} a[420000];
int start[220000], tot;
int n;
inline bool islucky(int n) {
  if (n < 10) return (n == 4) || (n == 7);
  return islucky(n % 10) && islucky(n / 10);
}
void add(int u, int v, int w) {
  a[tot].v = v;
  a[tot].w = (int)islucky(w);
  a[tot].next = start[u];
  start[u] = tot++;
}
int dfs(int u, int fa) {
  down[u] = 1;
  int c = 0;
  for (int i = start[u]; i != -1; i = a[i].next) {
    if (a[i].v == fa) continue;
    dfs(a[i].v, u);
    if (a[i].w)
      c += down[a[i].v];
    else
      c += cd[a[i].v];
    down[u] += down[a[i].v];
  }
  cd[u] = c;
  return cd[u];
}
void dfs1(int u, int fa) {
  for (int i = start[u]; i != -1; i = a[i].next) {
    if (a[i].v == fa) continue;
    int v = a[i].v;
    if (a[i].w)
      cu[v] += up[v];
    else {
      cu[v] += cu[u] + cd[u] - cd[v];
    }
    dfs1(v, u);
  }
}
int main() {
  int i, j, k;
  scanf("%d", &n);
  memset(start, -1, sizeof(start));
  tot = 0;
  for (i = 0; i < n - 1; i++) {
    int q, w, e;
    scanf("%d%d%d", &q, &w, &e);
    add(q, w, e);
    add(w, q, e);
  }
  cost[1] = dfs(1, -1);
  for (i = 1; i <= n; i++) {
    up[i] = down[1] - down[i];
    cost[i] = cd[i];
  }
  cu[1] = 0;
  dfs1(1, -1);
  for (i = 1; i <= n; i++) {
    cost[i] += cu[i];
  }
  long long ans = n * 1LL;
  ans *= (n - 1) * 1LL;
  ans *= (n - 2) * 1LL;
  ans = 0;
  for (i = 1; i <= n; i++) {
    long long t = cost[i];
    ans += t * (t - 1);
  }
  cout << ans << endl;
  return 0;
}
