#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
bool vis[100005];
long long cnt;
bool chk(int w) {
  while (w) {
    if (w % 10 != 7 && w % 10 != 4) return true;
    w /= 10;
  }
  return false;
}
void gao(int x) {
  vis[x] = 1;
  int i;
  cnt++;
  for (i = 0; i < v[x].size(); i++) {
    if (!vis[v[x][i]]) gao(v[x][i]);
  }
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int i, j;
    int t = n - 1;
    for (i = 1; i <= n; i++) v[i].clear();
    while (t--) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      if (chk(c)) {
        v[a].push_back(b);
        v[b].push_back(a);
      }
    }
    memset(vis, 0, sizeof(vis));
    long long ans = 0;
    for (i = 1; i <= n; i++) {
      if (!vis[i]) {
        cnt = 0;
        gao(i);
        ans += cnt * (n - cnt) * (n - cnt - 1);
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
