#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int n, i, u, v, w, t;
int mark[maxN];
long long int sum = 0;
vector<int> G[maxN];
bool lucky(int c) {
  while (c) {
    if (c % 10 != 4 && c % 10 != 7) {
      return false;
    }
    c /= 10;
  }
  return true;
}
int componentSize(int v) {
  mark[v] = 1;
  int cnt = 1;
  for (auto u : G[v]) {
    if (mark[u] != 1) {
      cnt += componentSize(u);
    }
  }
  return cnt;
}
int main() {
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    cin >> u >> v >> w;
    if (!lucky(w)) {
      G[u].push_back(v);
      G[v].push_back(u);
    }
  }
  for (i = 1; i <= n; i++) {
    if (mark[i] != 1) {
      t = componentSize(i);
      sum += (long long)t * (long long)(n - t) * (long long)(n - t - 1);
    }
  }
  cout << sum << endl;
  return 0;
}
