#include <bits/stdc++.h>
using namespace std;
int fa[(100000 + 10)], nums[(100000 + 10)];
bool vis[(100000 + 10)];
int finds(int x) {
  if (fa[x] == x) return x;
  return fa[x] = finds(fa[x]);
}
void init(int n) {
  for (int i = 1; i <= n; i++) {
    fa[i] = i;
    nums[i] = 1;
  }
}
int check(int x) {
  while (x) {
    int dig = x % 10;
    if (dig != 7 && dig != 4) return 0;
    x /= 10;
  }
  return 1;
}
long long mutil(long long a, long long b, long long c) {
  if (a < 0 || b < 0 || c < 0)
    return 0;
  else
    return a * b * c;
}
int main() {
  long long n;
  scanf("%lld", &n);
  init(n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    int tg = check(w);
    if (!tg) {
      int fu = finds(u), fv = finds(v);
      if (fu != fv) {
        nums[fu] += nums[fv];
        fa[fv] = fu;
      }
    }
  }
  long long ans = n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    int id = finds(i);
    if (!vis[id]) {
      vis[id] = true;
      ans -= mutil(nums[id], nums[id] - 1, nums[id] - 2);
      ans -= 2ll * mutil(nums[id], nums[id] - 1, n - nums[id]);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
