#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 10;
int n, comp[N], sz[N], ans;
vector<int> g[N];
inline bool lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) {
      return 0;
    }
    x /= 10;
  }
  return 1;
}
void dfs(int v, int C) {
  comp[v] = C;
  sz[C]++;
  for (int u : g[v]) {
    if (!comp[u]) {
      dfs(u, C);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    if (!lucky(w)) {
      g[u].push_back(v);
      g[v].push_back(u);
    }
  }
  for (int i = 0, C = 0; i < n; i++) {
    if (!comp[i]) {
      dfs(i, ++C);
    }
  }
  for (int i = 0; i < n; i++) {
    int C = n - sz[comp[i]];
    ans += C * (C - 1);
  }
  cout << ans << '\n';
}
