#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 14;
int par[MAXN], t[MAXN];
bool lucky(int w) {
  while (w != 0) {
    if (w % 10 != 4 && w % 10 != 7) return false;
    w /= 10;
  }
  return true;
}
int findRoot(int v) {
  if (par[v] == v) return v;
  return par[v] = findRoot(par[v]);
}
void dsu(int a, int b) {
  int r_a = findRoot(a), r_b = findRoot(b);
  if (r_a > r_b) swap(r_a, r_b);
  if (r_a != r_b) par[r_a] = r_b;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (!lucky(w)) dsu(u, v);
  }
  for (int i = 1; i <= n; i++) t[findRoot(i)]++;
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (t[i] != 0) ans += 1LL * t[i] * (n - t[i]) * (n - t[i] - 1);
  cout << ans << '\n';
  return 0;
}
