#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int p[maxn];
int find(int u) { return (u == p[u]) ? u : (p[u] = find(p[u])); }
int cnt[maxn];
int countt, n;
int is(int w) {
  char a[100];
  sprintf(a, "%d", w);
  string s(a);
  if (count(s.begin(), s.end(), '4') + count(s.begin(), s.end(), '7') ==
      s.length())
    return 1;
  return 0;
}
int main(void) {
  int i, from, to, w;
  while (cin >> n) {
    for (i = 1; i <= n; i++) {
      p[i] = i;
    }
    countt = 0;
    for (i = 1; i < n; i++) {
      cin >> from >> to >> w;
      if (is(w)) continue;
      int x = find(from);
      int y = find(to);
      if (x != y) p[x] = y;
    }
    for (i = 1; i <= n; i++) p[i] = find(i);
    vector<int> V;
    memset(cnt, 0, sizeof(cnt));
    for (i = 1; i <= n; i++) cnt[p[i]]++;
    int N = 0;
    for (i = 1; i <= n; i++)
      if (cnt[i]) {
        V.push_back(cnt[i]);
        N += cnt[i];
      }
    long long cnt = 0;
    for (i = 0; i < V.size(); i++) {
      long long a = V[i];
      long long b = N - V[i];
      cnt += a * b * (b - 1);
    }
    cout << cnt << endl;
  }
  return 0;
}
