#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long n, u, v, w, ans, cnt;
bool mark[N];
vector<int> adj[N];
bool isLucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
void dfs(int v) {
  mark[v] = 1;
  cnt++;
  for (auto u : adj[v])
    if (!mark[u]) dfs(u);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    if (!isLucky(w)) {
      adj[u].push_back(v);
      adj[v].push_back(u);
    }
  }
  ans = n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      cnt = 0;
      dfs(i);
      ans -= cnt * (cnt - 1) * (cnt - 2);
      ans -= 2 * cnt * (cnt - 1) * (n - cnt);
    }
  }
  cout << ans;
  return 0;
}
