#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
vector<pair<int, bool> > nbrs[MAX_N];
int ps[MAX_N], pes[MAX_N], cns[MAX_N];
vector<int> clds[MAX_N];
int szs[MAX_N], lszs[MAX_N], plszs[MAX_N];
bool lucky(int n) {
  if (n <= 0) return false;
  while (n) {
    int d = n % 10;
    if (d != 4 && d != 7) return false;
    n /= 10;
  }
  return true;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    a--, b--;
    bool l = lucky(w);
    nbrs[a].push_back(pair<int, bool>(b, l));
    nbrs[b].push_back(pair<int, bool>(a, l));
  }
  ps[0] = -1, pes[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int &up = ps[u];
    vector<pair<int, bool> > &nbru = nbrs[u];
    for (vector<pair<int, bool> >::iterator vit = nbru.begin();
         vit != nbru.end(); vit++) {
      int &v = vit->first;
      if (v != up) {
        clds[u].push_back(v);
        cns[u]++;
        ps[v] = u;
        pes[v] = vit->second;
        q.push(v);
      }
    }
  }
  for (int u = 0; u < n; u++)
    if (cns[u] == 0) q.push(u);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    szs[u]++;
    int &up = ps[u], &upe = pes[u];
    if (up >= 0) {
      szs[up] += szs[u];
      lszs[up] += upe ? szs[u] : lszs[u];
      if (--cns[up] == 0) q.push(up);
    }
  }
  plszs[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vector<int> &cldu = clds[u];
    for (vector<int>::iterator vit = cldu.begin(); vit != cldu.end(); vit++) {
      int &v = *vit;
      if (pes[v])
        plszs[v] = szs[0] - szs[v];
      else
        plszs[v] = plszs[u] + lszs[u] - lszs[v];
      q.push(v);
    }
  }
  long long sum = 0;
  for (int u = 0; u < n; u++) {
    int lsu = lszs[u] + plszs[u];
    sum += (long long)lsu * (lsu - 1);
  }
  printf("%lld\n", sum);
  return 0;
}
