#include <bits/stdc++.h>
using namespace std;
vector<pair<int, bool> > tree[100005];
bool v[100005];
bool is_lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int dfs(int node, int parent) {
  v[node] = true;
  int cnt = 1;
  for (int i = 0; i < tree[node].size(); i++) {
    int x = tree[node][i].first;
    if (x == parent || tree[node][i].second) continue;
    cnt += dfs(x, node);
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, x, y, z;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y >> z;
    z = is_lucky(z);
    tree[x].push_back(make_pair(y, z));
    tree[y].push_back(make_pair(x, z));
  }
  long long res = 1LL * n * (n - 1) * (n - 2);
  for (int i = 1; i <= n; i++) {
    if (v[i]) continue;
    int cnt = dfs(i, i);
    long long nw = 1LL * cnt * (cnt - 1) * (n - cnt) * 2;
    nw += 1LL * cnt * (cnt - 1) * (cnt - 2);
    res -= nw;
  }
  cout << res;
  return 0;
}
