#include <bits/stdc++.h>
using namespace std;
const int iz = 1e5 + 5;
int n;
int h[iz], to[iz << 1], nex[iz << 1], w[iz << 1], tot = 0;
inline void add(int x, int y, int w3) {
  to[++tot] = y;
  w[tot] = w3;
  nex[tot] = h[x];
  h[x] = tot;
}
int s[iz], f1[iz], f2[iz];
long long ans = 0;
void dfs(int u, int fa) {
  s[u] = 1;
  for (int i = h[u]; i; i = nex[i]) {
    if (to[i] == fa) continue;
    dfs(to[i], u);
    s[u] += s[to[i]];
    if (w[i] == 1)
      f1[u] += s[to[i]];
    else
      f1[u] += f1[to[i]];
  }
}
void dfs2(int u, int fa) {
  for (int i = h[u], v; i; i = nex[i]) {
    if ((v = to[i]) == fa) continue;
    if (w[i] == 1)
      f2[v] = n - s[v];
    else
      f2[v] = f2[u] + f1[u] - f1[v];
    dfs2(v, u);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1, x, y, w2; i < n; i++) {
    scanf("%d%d%d", &x, &y, &w2);
    int si = 1;
    while (w2 > 0) {
      if (w2 % 10 != 4 && w2 % 10 != 7) si = 0;
      w2 /= 10;
    }
    add(x, y, si);
    add(y, x, si);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) {
    ans += 1ll * f1[i] * (f1[i] - 1) + 1ll * f2[i] * (f2[i] - 1) +
           2 * 1ll * f1[i] * f2[i];
  }
  printf("%lld", ans);
}
