#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
long long n, head[MAXN], cnt, f[MAXN], d[MAXN], siz[MAXN];
struct Edge {
  int v, w, next;
} edge[MAXN * 2];
void add(int u, int v, int w) {
  cnt++;
  edge[cnt].v = v;
  edge[cnt].next = head[u];
  edge[cnt].w = w;
  head[u] = cnt;
}
int check(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
void dfs1(int u, int fa) {
  siz[u] = 1;
  f[u] = 0;
  for (int i = head[u]; ~i; i = edge[i].next) {
    int v = edge[i].v;
    if (v == fa) continue;
    dfs1(v, u);
    siz[u] += siz[v];
    if (edge[i].w)
      f[u] += siz[v];
    else
      f[u] += f[v];
  }
}
void dfs2(int u, int fa) {
  for (int i = head[u]; ~i; i = edge[i].next) {
    int v = edge[i].v;
    if (v == fa) continue;
    if (edge[i].w)
      d[v] = n - siz[v];
    else
      d[v] = d[u] + f[u] - f[v];
    dfs2(v, u);
  }
}
int main() {
  memset(head, -1, sizeof head);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    w = check(w);
    add(u, v, w);
    add(v, u, w);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += f[i] * (f[i] - 1) + d[i] * (d[i] - 1) + 2 * f[i] * d[i];
  cout << ans << endl;
  return 0;
}
