#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e16;
const int maxn = 3e5 + 1;
int n;
long long size[maxn];
int link[maxn];
bool vis[maxn];
bool lucky(int x) {
  while (x != 0) {
    if (x % 10 != 4 && x % 10 != 7) {
      return false;
    }
    x /= 10;
  }
  return true;
}
int find(int x) {
  while (x != link[x]) {
    x = link[x];
  }
  return x;
}
bool same(int a, int b) { return find(a) == find(b); }
void unite(int a, int b) {
  a = find(a);
  b = find(b);
  if (size[a] < size[b]) {
    swap(a, b);
  }
  size[a] += size[b];
  link[b] = a;
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    size[i] = 1;
    link[i] = i;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    if (!lucky(w)) {
      unite(u, v);
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int rep = find(i);
    if (!vis[rep]) {
      vis[rep] = true;
      ans += size[rep] * (n - size[rep]) * (n - size[rep] - 1);
    }
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
