#include <bits/stdc++.h>
template <typename T>
T inline SQR(const T& a) {
  return a * a;
}
template <typename T>
T inline ABS(const T& a) {
  return a < 0 ? -a : a;
}
template <typename T>
T inline MIN(const T& a, const T& b) {
  if (a < b) return a;
  return b;
}
template <typename T>
T inline MAX(const T& a, const T& b) {
  if (a > b) return a;
  return b;
}
const double EPS = 1e-9;
inline int SGN(double a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); }
inline int CMP(double a, double b) { return SGN(a - b); }
using namespace std;
int pa[100000], rk[100000];
long long sz[100000];
int find(int x) {
  int a = x;
  while (pa[a] != a) a = pa[a];
  int y;
  while (pa[x] != a) {
    y = pa[x];
    pa[x] = a;
    x = y;
  }
  return a;
}
void fUnion(int x, int y) {
  int px = find(x);
  int py = find(y);
  if (px == py) return;
  if (rk[py] > rk[px]) {
    px ^= py;
    py ^= px;
    px ^= py;
  }
  if (rk[px] == rk[py]) rk[px]++;
  pa[py] = px;
  sz[px] += sz[py];
}
bool lucky(int N) {
  while (N) {
    if (N % 10 != 4 && N % 10 != 7) return false;
    N /= 10;
  }
  return true;
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  int N, u, v, w;
  cin >> N;
  for (int i = 0; i < N; ++i) pa[i] = i, rk[i] = 0, sz[i] = 1;
  for (int i = 0; i < N - 1; ++i) {
    cin >> u >> v >> w;
    u--, v--;
    if (!lucky(w)) fUnion(u, v);
  }
  long long ans = 0;
  for (int i = 0; i < N; ++i)
    if (i == find(i)) ans += sz[i] * (N - sz[i]) * (N - sz[i] - 1);
  cout << ans << "\n";
  return 0;
}
