#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long a[N];
bool isLucky(int x) {
  while (x) {
    int d = x % 10;
    x /= 10;
    if (!(d == 4 || d == 7)) return 0;
  }
  return 1;
}
vector<pair<int, bool> > v[N];
int sz[N];
long long down[N], up[N];
void calcdown(int s, int p) {
  sz[s] = 1;
  for (auto i : v[s]) {
    int x = i.first, f = i.second;
    if (x != p) {
      calcdown(x, s);
      sz[s] += sz[x];
      if (f) {
        down[s] += sz[x];
      } else {
        down[s] += down[x];
      }
    }
  }
}
void calcup(int s, int p) {
  for (auto i : v[s]) {
    int x = i.first, f = i.second;
    if (x != p) {
      if (f) {
        up[x] += sz[1] - sz[x];
      } else {
        up[x] += up[s] + down[s] - down[x];
      }
      calcup(x, s);
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    z = isLucky(z);
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  calcdown(1, 0);
  calcup(1, 0);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (down[i] * (down[i] - 1)) + (up[i] * (up[i] - 1)) +
           ((up[i] * down[i]) << 1LL);
  }
  cout << ans;
  return 0;
}
