#include <bits/stdc++.h>
using namespace std;
const int64_t N = 1e5 + 1e2;
int64_t n, cnt[N], ans;
bool mark[N];
vector<pair<int64_t, int64_t> > adj[N];
vector<int64_t> mol, molt;
bool lucky(int64_t a) {
  while (a) {
    if (a % 10 != 7 && a % 10 != 4) {
      return 0;
    }
    a /= 10;
  }
  return 1;
}
void dfs(int64_t v) {
  mark[v] = true;
  mol.push_back(v);
  for (int64_t i = 0; i < adj[v].size(); i++) {
    if (mark[adj[v][i].first] || 1 == adj[v][i].second) continue;
    dfs(adj[v][i].first);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  int64_t v, u, w;
  for (int64_t i = 0; i < n - 1; i++) {
    cin >> v >> u >> w;
    if (lucky(w))
      w = 1;
    else
      w = 0;
    adj[v].push_back({u, w});
    adj[u].push_back({v, w});
  }
  for (int64_t i = 1; i <= n; i++) {
    if (mark[i]) continue;
    mol.clear();
    dfs(i);
    cnt[i] = n - mol.size();
    ans += mol.size() * cnt[i] * (cnt[i] - 1);
  }
  cout << ans;
}
