#include <bits/stdc++.h>
using namespace std;
struct dsu {
  vector<long long> parent, size;
  dsu(long long _n) {
    parent.resize(_n + 1);
    iota(parent.begin(), parent.end(), 0);
    size.assign(_n + 1, 1);
  }
  long long find_set(long long v) {
    if (v == parent[v])
      return v;
    else {
      long long result = find_set(parent[v]);
      parent[v] = result;
      return result;
    }
  }
  bool union_sets(long long a, long long b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
      if (size[a] < size[b]) swap(a, b);
      parent[b] = a, size[a] += size[b];
    } else
      return false;
    return true;
  }
  long long get_size(long long p) { return size[find_set(p)]; }
};
void solve() {
  long long n;
  cin >> n;
  auto check = [](long long n) {
    while (n > 0) {
      if (n % 10 == 4 or n % 10 == 7) {
        n /= 10;
        continue;
      }
      return false;
    }
    return true;
  };
  dsu d(n + 2);
  for (long long i = 0; i < n - 1; ++i) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (!check(w)) {
      d.union_sets(u, v);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) {
    long long p = n - d.get_size(i);
    ans += (p * (p - 1));
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  t = 1;
  for (long long i = 1; i <= t; ++i) {
    solve();
  }
  return 0;
}
