#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int N = 100010;
int pnt[N], cnt[N];
int n;
bool check(int x) {
  while (x) {
    if (x % 10 == 4 || x % 10 == 7) {
      x /= 10;
    } else {
      return false;
    }
  }
  return true;
}
int find(int x) {
  if (x == pnt[x]) return x;
  return pnt[x] = find(pnt[x]);
}
void merge(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) return;
  pnt[u] = v;
  cnt[v] += cnt[u];
}
void solve() {
  int u, v, w;
  for (int i = 0; i < (int)(n + 1); ++i) {
    pnt[i] = i;
    cnt[i] = 1;
  }
  for (int i = 0; i < (int)(n - 1); ++i) {
    scanf("%d%d%d", &u, &v, &w);
    if (!check(w)) merge(u, v);
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    long long tot = n - cnt[find(i)];
    if (tot >= 2) ans += tot * (tot - 1);
  }
  printf(
      "%I64d"
      "\n",
      ans);
}
int main() {
  while (~scanf("%d", &n)) {
    solve();
  }
  return 0;
}
