#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 6;
const long long MOD = 1e9 + 7;
int par[MAX];
long long sz[MAX];
long long triplets(long long x) { return x * (x - 1) * (x - 2); }
bool isLucky(long long x) {
  while (x) {
    if (x % 10 != 4 and x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int Find(int u) {
  if (u == par[u]) return u;
  return par[u] = Find(par[u]);
}
void Union(int u, int v) {
  u = Find(u);
  v = Find(v);
  if (sz[u] < sz[v]) swap(u, v);
  if (u != v) {
    par[v] = u;
    sz[u] += sz[v];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1;
  for (int i = 1; i <= n - 1; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (!isLucky(w)) Union(u, v);
  }
  long long ans = n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    if (Find(i) == i) {
      ans -= 2 * sz[i] * (sz[i] - 1) * (n - sz[i]) +
             sz[i] * (sz[i] - 1) * (sz[i] - 2);
    }
  }
  cout << ans;
}
