#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long int N = 2e5 + 30, mod = 1e9 + 7, inf = 1e9;
vector<pair<int, int> > adj[N];
long long int good[N], child[N], beg[N];
int n;
bool luck(int n) {
  while (n > 0) {
    if (n % 10 != 4 && n % 10 != 7) return 0;
    n /= 10;
  }
  return 1;
}
void pre(int v, int p) {
  long long int ans = 0;
  for (auto u : adj[v]) {
    if (u.first != p) {
      pre(u.first, v);
      if (!luck(u.second))
        good[v] += good[u.first];
      else
        good[v] += child[u.first];
      child[v] += child[u.first];
    }
  }
  child[v]++;
}
void dfs(int v, int p, int edge) {
  if (luck(edge))
    beg[v] = (n - child[v]) * (n - child[v] - 1) + good[v] * (good[v] - 1) +
             good[v] * (n - child[v]) * 2;
  else
    beg[v] = beg[p];
  for (auto u : adj[v]) {
    if (u.first != p) {
      dfs(u.first, v, u.second);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
  }
  pre(1, -1);
  dfs(1, -1, 4);
  long long int ans = 0;
  for (int i = 1; i < n + 1; i++) ans += beg[i];
  cout << ans;
  cerr << "beg[2]"
       << ": " << beg[2] << '\n';
  ;
  return 0;
}
