#include <bits/stdc++.h>
using namespace std;
const long long MX = 1e5 + 9;
long long par[MX], num[MX];
void MS(long long v) { par[v] = v, num[v] = 1; }
long long FS(long long v) {
  if (par[v] == v) return v;
  return par[v] = FS(par[v]);
}
void US(long long a, long long b) {
  a = FS(a), b = FS(b);
  if (a == b) return;
  if (num[a] < num[b]) swap(a, b);
  par[b] = a;
  num[a] += num[b];
}
bool lucky(string s) {
  for (long long i = 0; i < s.size(); i++)
    if (s[i] != '4' && s[i] != '7') return 0;
  return 1;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) MS(i);
  for (long long i = 1; i < n; i++) {
    long long u, v;
    string s;
    cin >> u >> v >> s;
    if (!lucky(s)) US(u, v);
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long sz = n - num[FS(i)];
    ans += sz * (sz - 1);
  }
  cout << ans;
  return 0;
}
