#include <bits/stdc++.h>
using namespace std;
vector<int> a[100005];
int vis[100005] = {0};
int comp = 0;
void dfs(int n) {
  vis[n] = 1;
  comp++;
  for (int i = 0; i < a[n].size(); i++) {
    if (vis[a[n][i]] == 0) {
      dfs(a[n][i]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int t1, t2, w;
  for (int i = 0; i < n - 1; i++) {
    cin >> t1 >> t2 >> w;
    int w1 = w, flag = 0;
    while (w1) {
      if (w1 % 10 != 4 && w1 % 10 != 7) {
        flag = 1;
        break;
      }
      w1 /= 10;
    }
    if (flag == 1) {
      a[t1].push_back(t2);
      a[t2].push_back(t1);
    }
  }
  vector<long long> golu;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      comp = 0;
      dfs(i);
      golu.push_back(comp);
    }
  }
  long long ans = 0;
  for (int i = 0; i < golu.size(); i++) {
    long long temp = n - golu[i];
    if (temp <= 1) continue;
    temp = temp * (temp - 1);
    temp *= golu[i];
    ans += temp;
  }
  cout << ans << endl;
  return 0;
}
