#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> gr[(ll)(1e5 + 3)];
int nn = 0;
set<int> se;
void dfs(int node, int par) {
  nn++;
  se.erase(node);
  for (int x : gr[node]) {
    if (x != par) {
      dfs(x, node);
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  se.insert(n);
  for (int i = 1; i < n; i++) {
    se.insert(i);
    int a, b, w;
    cin >> a >> b >> w;
    string ss = to_string(w);
    int cc = 0;
    for (char x : ss) {
      cc += (x == '4' || x == '7');
    }
    if (cc != ss.size()) {
      gr[a].push_back(b);
      gr[b].push_back(a);
    }
  }
  ll ans = 0;
  while (se.size()) {
    nn = 0;
    dfs(*se.begin(), 0);
    ll xx = n - nn;
    ans += (xx) * (xx - 1) * nn;
  }
  cout << ans << "\n";
}
