#include <bits/stdc++.h>
using namespace std;
namespace io {
inline void readl(long long &x) {
  x = 0;
  long long f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
inline void read(int &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -f;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
}  // namespace io
using namespace io;
int n, en;
int front[100005], sum[100005], f1[100005], f2[100005];
long long ans;
struct edge {
  int v, w, next;
} e[100005 << 1];
inline void add(int u, int v, int w) {
  en++;
  e[en].v = v;
  e[en].w = w;
  e[en].next = front[u];
  front[u] = en;
}
bool cal(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
void dfs1(int u, int fa) {
  sum[u] = 1;
  for (register int i = front[u]; i; i = e[i].next) {
    int v = e[i].v, w = e[i].w;
    if (v == fa) continue;
    dfs1(v, u);
    sum[u] += sum[v];
    if (w)
      f1[u] += sum[v];
    else
      f1[u] += f1[v];
  }
}
void dfs2(int u, int fa) {
  for (register int i = front[u]; i; i = e[i].next) {
    int v = e[i].v, w = e[i].w;
    if (v == fa) continue;
    if (w)
      f2[v] = sum[1] - sum[v];
    else
      f2[v] = f1[u] + f2[u] - f1[v];
    dfs2(v, u);
  }
}
int main() {
  int u, v, w;
  read(n);
  for (register int i = 1; i <= n - 1; i++)
    read(u), read(v), read(w), add(u, v, cal(w)), add(v, u, cal(w));
  dfs1(1, 1);
  dfs2(1, 1);
  for (register int i = 1; i <= n; i++)
    ans += 1ll * f1[i] * (f1[i] - 1) + 1ll * f2[i] * (f2[i] - 1) +
           1ll * f1[i] * f2[i] * 2;
  printf("%lld", ans);
  return 0;
}
