#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool mod(long double a, long double b) { return a / b - floor(a / b); }
const long long N = 1e5 + 5;
long long dp[N][2];
bool lucky(long long n) {
  bool islucky = true;
  while (n > 0) {
    long long first = n % 10;
    n /= 10;
    if (first != 4 && first != 7) islucky = false;
  }
  return islucky;
}
map<pair<long long, long long>, long long> mp;
vector<long long> graph[N];
long long sum, vis[N];
long long dfs(long long p, long long par) {
  if (vis[p] != -1) return 0;
  long long first = 0;
  vis[p] = 1;
  for (long long aa : graph[p]) {
    if (aa == par) continue;
    first += dfs(aa, p);
  }
  return first + 1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, first, n;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (!lucky(c)) {
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
  }
  memset(vis, -1, sizeof(vis));
  sum = 0;
  for (i = 1; i < n + 1; i++) {
    if (vis[i] == -1) {
      long long a, b;
      a = dfs(i, -1);
      b = n - a;
      sum += (a * b * (b - 1));
    }
  }
  cout << sum << '\n';
  return 0;
}
