#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10, mod = 1e9 + 7;
long long fact[maxn], inv[maxn];
long long par[maxn], sz[maxn];
long long bigmod(long long a, long long b, long long m) {
  if (b == 0) return 1;
  long long x = bigmod(a, b / 2, m);
  x = (x * x) % mod;
  if (b & 1) x = (x * a) % mod;
  return x;
}
void precalc() {
  fact[0] = inv[0] = 1;
  for (long long i = 1; i < maxn; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    inv[i] = bigmod(fact[i], mod - 2, mod);
  }
  for (long long i = 1; i < maxn; i++) par[i] = i;
}
long long find(long long x) {
  if (x == par[x]) return x;
  return par[x] = find(par[x]);
}
void join(long long a, long long b) { par[find(b)] = find(a); }
long long f(long long x) {
  if (x < 2) return 0;
  long long ret = fact[x];
  ret = (ret * inv[x - 2]) % mod;
  return ret;
}
bool ok(long long n) {
  while (n) {
    long long rem = n % 10;
    if (rem != 4 && rem != 7) return 1;
    n /= 10;
  }
  return 0;
}
int main() {
  precalc();
  long long n;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long x, y, c;
    cin >> x >> y >> c;
    if (ok(c)) join(x, y);
  }
  if (n < 3) {
    cout << 0 << endl;
    return 0;
  }
  set<long long> cmp;
  for (long long i = 1; i <= n; i++) sz[find(i)]++, cmp.insert(find(i));
  long long ans = 0;
  for (auto x : cmp) {
    long long size = sz[find(x)];
    long long tmp = (n - size);
    tmp = (tmp * (n - size - 1));
    tmp = (tmp * size);
    ans = (ans + tmp);
  }
  cout << ans << endl;
  return 0;
}
