#include <bits/stdc++.h>
using namespace std;
int isLucky(int a) {
  return a == 0 || (a % 10 == 4 || a % 10 == 7) && isLucky(a / 10);
}
vector<int> adj[100010];
bool vis[100010];
long long ans = 0, m;
void dfs(int a) {
  m++;
  vis[a] = true;
  for (int i = 0; i < adj[a].size(); i++) {
    if (!vis[adj[a][i]]) {
      dfs(adj[a][i]);
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (!isLucky(c)) {
      adj[a].push_back(b);
      adj[b].push_back(a);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      m = 0;
      dfs(i);
      ans += m * ((n - m) * (n - m - 1));
    }
  }
  cout << ans;
}
