#include <bits/stdc++.h>
using namespace std;
vector<int> ke[100005];
bool visited[100005];
long long ans, cnt, tmp;
int n;
bool isLucky(int n) {
  while (n > 0) {
    if (n % 10 != 4 && n % 10 != 7) return false;
    n /= 10;
  }
  return true;
}
void DFS(int u) {
  visited[u] = true;
  ++cnt;
  for (vector<int>::iterator it = ke[u].begin(); it != ke[u].end(); it++)
    if (!visited[*it]) DFS(*it);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    if (!isLucky(w)) {
      ke[u].push_back(v);
      ke[v].push_back(u);
    }
  }
  tmp = n;
  ans = tmp * (tmp - 1) * (tmp - 2);
  for (int u = 1; u <= n; ++u)
    if (!visited[u]) {
      cnt = 0;
      DFS(u);
      ans -= 2 * cnt * (cnt - 1) * (tmp - cnt) + cnt * (cnt - 1) * (cnt - 2);
    }
  printf("%I64d", ans);
  return 0;
}
