#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int par[maxn];
int siz[maxn];
bool is_lucky(int v) {
  if (v < 10) {
    if (v == 4 || v == 7)
      return 1;
    else
      return 0;
  } else {
    return (is_lucky(v / 10) && is_lucky(v % 10));
  }
}
int find_par(int v) {
  if (par[v] == v)
    return v;
  else {
    par[v] = find_par(par[v]);
    return par[v];
  }
}
int n;
long long ans = 0;
void merge_it(int v, int s) {
  if (find_par(s) == find_par(v)) return;
  int vp = find_par(v);
  int vs = find_par(s);
  if (siz[vs] > siz[vp]) swap(vs, vp);
  siz[vp] += siz[vs];
  siz[vs] = 0;
  par[vs] = vp;
}
vector<int> adj[maxn];
bool check[maxn];
void dfs(int v) {
  check[v] = 1;
  long long x = siz[v];
  x *= (n - siz[v]);
  x *= (n - siz[v] - 1);
  ans += x;
  for (auto u : adj[v]) {
    if (check[u]) continue;
    dfs(u);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  vector<pair<int, int> > inp;
  for (int i = 0; i < maxn; i++) {
    par[i] = i;
    siz[i] = 1;
  }
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, x;
    cin >> u >> v >> x;
    if (is_lucky(x)) {
      inp.push_back({u, v});
    } else {
      merge_it(u, v);
    }
  }
  for (int i = 0; i < inp.size(); i++) {
    int s1 = inp[i].first;
    int s2 = inp[i].second;
    s1 = find_par(s1);
    s2 = find_par(s2);
    adj[s1].push_back(s2);
    adj[s2].push_back(s1);
  }
  dfs(find_par(1));
  cout << ans;
}
