#include <bits/stdc++.h>
using namespace std;
const int oo = 999999999;
const double PI = 3.1415931;
const double eps = 1e-9;
const int maxN = 100000 + 10;
int n, par[maxN], sz[maxN];
bool islucky(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int find(int x) {
  if (par[x] != x) par[x] = find(par[x]);
  return par[x];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
  for (int i = 1; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (!islucky(z)) {
      x = find(x);
      y = find(y);
      par[x] = y;
      sz[y] += sz[x];
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (long long)(n - sz[find(i)]) * (long long)(n - sz[find(i)] - 1);
  }
  cout << ans << endl;
  return 0;
}
