#include <bits/stdc++.h>
using namespace std;
vector<int> L[100005], W[100005];
bool vis[100005];
long long comp;
bool isLucky(int n) {
  while (n > 0) {
    if (n % 10 != 4 && n % 10 != 7) return false;
    n /= 10;
  }
  return true;
}
void DFS(int node) {
  if (vis[node]) return;
  vis[node] = true;
  comp++;
  for (int i = 0; i < L[node].size(); i++) {
    int to = L[node][i];
    int w = W[node][i];
    if (isLucky(w)) continue;
    DFS(to);
  }
}
int main() {
  long long n;
  while (cin >> n) {
    int x, y, w;
    for (int i = 0; i < n - 1; i++) {
      cin >> x >> y >> w;
      x--, y--;
      L[x].push_back(y);
      L[y].push_back(x);
      W[x].push_back(w);
      W[y].push_back(w);
    }
    long long ans = n * (n - 1) * (n - 2);
    memset(vis, false, sizeof vis);
    for (int i = 0; i < n; i++) {
      if (vis[i]) continue;
      comp = 0;
      DFS(i);
      ans -=
          2 * comp * (comp - 1) * (n - comp) + comp * (comp - 1) * (comp - 2);
    }
    cout << ans << endl;
  }
}
