#include <bits/stdc++.h>
using namespace std;
long long vis[100005], n, up[100005], below[100005], vet[100005], p[100005],
    ans = 0;
vector<pair<long long, long long> > v[100005];
long long c(long long x) { return (x * (x - 1)); }
bool check(long long x) {
  while (x != 0) {
    long long p = x % 10;
    if (p != 4 and p != 7) {
      return false;
    }
    x = x / 10;
  }
  return true;
}
void dfs(int node, int par) {
  vis[node] = 1;
  p[node] = par;
  for (auto it : v[node]) {
    if (!vis[it.first]) {
      dfs(it.first, node);
      if (it.second == 1) {
        below[node] += vet[it.first];
      } else {
        below[node] += below[it.first];
      }
      vet[node] += vet[it.first];
    }
  }
  vet[node]++;
  ;
}
void dfs2(int node, int status) {
  vis[node] = 1;
  if (node == 1) {
    ans += c(below[node]);
  } else {
    if (status == 1) {
      up[node] = n - vet[node];
      ans += c(below[node] + up[node]);
    } else {
      up[node] = up[p[node]];
      up[node] += (below[p[node]] - below[node]);
      ans += c(below[node] + up[node]);
    }
  };
  for (auto it : v[node]) {
    if (!vis[it.first]) {
      dfs2(it.first, it.second);
    }
  }
}
int main() {
  long long i, a, b, c;
  scanf("%lld", &n);
  ;
  for (i = 0; i < n - 1; i++) {
    scanf("%lld %lld %lld", &a, &b, &c);
    ;
    if (check(c)) {
      ;
      v[a].push_back(make_pair(b, 1));
      v[b].push_back(make_pair(a, 1));
    } else {
      v[a].push_back(make_pair(b, 0));
      v[b].push_back(make_pair(a, 0));
    }
  }
  dfs(1, 0);
  memset(vis, 0, sizeof(vis));
  dfs2(1, 0);
  cout << ans;
}
