#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long sum = 0, f = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (ch <= '9' && ch >= '0') sum = sum * 10 + ch - '0', ch = getchar();
  return sum * f;
}
bool pan(long long w) {
  while (w) {
    long long t = w % 10;
    if (t != 4 && t != 7) return false;
    w /= 10;
  }
  return true;
}
long long p, head[100010], last[100010 << 1], v[100010 << 1];
bool zhi[100010 << 1];
void add(long long a, long long b, bool w) {
  last[++p] = head[a];
  v[p] = b;
  head[a] = p;
  zhi[p] = w;
}
long long l[100010];
long long size[100010];
long long R[100010];
long long sum;
void dfs2(long long a, long long father) {
  for (long long i = head[a]; i; i = last[i])
    if (v[i] != father) {
      long long t = sum - size[v[i]];
      if (zhi[i])
        R[v[i]] = t;
      else
        R[v[i]] = R[a] + l[a] - l[v[i]];
      dfs2(v[i], a);
    }
}
void dfs(long long a, long long father) {
  size[a] = 1;
  for (long long i = head[a]; i; i = last[i])
    if (v[i] != father) {
      dfs(v[i], a);
      size[a] += size[v[i]];
      if (zhi[i])
        l[a] += size[v[i]];
      else
        l[a] += l[v[i]];
    }
}
long long n;
signed main() {
  n = read();
  for (long long i = 1; i < n; i++) {
    long long l = read(), r = read(), w = read();
    long long t = pan(w);
    add(l, r, t);
    add(r, l, t);
  }
  dfs(1, 0);
  sum = size[1];
  for (long long i = head[1]; i; i = last[i]) {
    if (zhi[i])
      R[v[i]] = size[1] - size[v[i]];
    else
      R[v[i]] = l[1] - l[v[i]];
    dfs2(v[i], 1);
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++)
    ans += R[i] * (R[i] - 1) + l[i] * (l[i] - 1) + R[i] * l[i] * 2;
  cout << ans;
  return 0;
}
