#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int N;
bool vis[MAX];
vector<pair<int, bool> > neigh[MAX];
bool islucky(int a) {
  while (a > 0) {
    if (a % 10 != 4 and a % 10 != 7) return false;
    a /= 10;
  }
  return true;
}
long long p3(long long N) { return (N) * (N - 1) * (N - 2); }
long long solve(int a) {
  vector<int> q;
  int qnum = 0;
  q.push_back(a);
  while (qnum < q.size()) {
    int cur = q[qnum];
    vis[cur] = true;
    for (int i = 0; i < neigh[cur].size(); i++) {
      if (neigh[cur][i].second) continue;
      int cneigh = neigh[cur][i].first;
      if (vis[cneigh]) continue;
      q.push_back(cneigh);
    }
    qnum++;
  }
  long long sz = q.size();
  return (2 * (sz) * (sz - 1) * ((long long)N - sz) + p3(sz));
}
int main() {
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    bool il = islucky(c);
    neigh[a].push_back(pair<int, bool>(b, il));
    neigh[b].push_back(pair<int, bool>(a, il));
  }
  long long ans = p3(N);
  for (int i = 0; i < MAX; i++) vis[i] = false;
  for (int i = 0; i < N; i++) {
    if (vis[i]) continue;
    ans -= solve(i);
  }
  cout << ans << endl;
}
