#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long N = 2e+5 + 5;
const long long mod = 1e+9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
long long p[N], sz[N];
long long n, m, q, u, v, w;
string s[N];
void make() {
  for (int i = 1; i <= n + m; i++) {
    p[i] = i;
    sz[i] = 1;
  }
}
int find(long long first) {
  return (p[first] == first) ? first : p[first] = find(p[first]);
}
void merge(long long first, long long second) {
  long long a = find(first);
  long long b = find(second);
  if (a != b) {
    if (sz[a] >= sz[b]) swap(a, b);
    p[a] = b;
    sz[b] += sz[a];
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed;
  cout << setprecision(10);
  ;
  cin >> n;
  make();
  for (int i = 0; i < n - 1; i++) {
    cin >> u >> v >> w;
    int flag = 1;
    while (w > 0) {
      if (w % 10 == 4 || w % 10 == 7) {
        w = w / 10;
      } else {
        flag = 0;
        break;
      }
    }
    if (flag == 0) merge(u, v);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (find(i) == i) {
      long long ct = n - sz[i];
      ans += ct * (ct - 1) * sz[i];
    }
  }
  cout << ans << endl;
}
