#include <bits/stdc++.h>
using namespace std;
bool islucky(int n) {
  while (n) {
    if ((n % 10 != 4) && (n % 10 != 7)) return false;
    n /= 10;
  }
  return true;
}
vector<int> G[110000];
bool was[110000];
int getCompSize(int start) {
  if (was[start]) return 0;
  was[start] = true;
  int res = 1;
  for (int i = 0; i < G[start].size(); i++) res += getCompSize(G[start][i]);
  return res;
}
int main() {
  long long n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    if (!islucky(w)) {
      G[a].push_back(b);
      G[b].push_back(a);
    }
  }
  long long res = 0;
  for (int i = 1; i <= n; i++)
    if (!was[i]) {
      long long compSize = getCompSize(i);
      if (compSize < n - 1)
        res += compSize * (n - compSize) * (n - compSize - 1);
    }
  cout << res;
exit:
  return (0);
}
