#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5 + 5;
const long long inf = 1e18;
const int mod = 998244353;
inline int pw(int x, int y) {
  int r = 1;
  while (y) {
    if (y & 1) r = 1LL * r * x % mod;
    x = 1LL * x * x % mod;
    y >>= 1;
  }
  return r;
}
bool lucky(long long k) {
  while (k) {
    if (k % 10 != 4 && k % 10 != 7) return false;
    k /= 10;
  }
  return true;
}
long long n, u, v, w, cnt = 0, ans = 0;
vector<long long> g[mxn];
bitset<mxn> vis;
void dfs(long long v) {
  vis[v] = 1;
  cnt++;
  for (auto u : g[v])
    if (!vis[u]) dfs(u);
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    if (!lucky(w)) {
      g[u].push_back(v);
      g[v].push_back(u);
    }
  }
  vis.reset();
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    if (vis[i] == 0) {
      dfs(i);
      ans += cnt * (n - cnt) * (n - cnt - 1);
    }
  }
  cout << ans << '\n';
  ;
  return 0;
}
