#include <bits/stdc++.h>
using namespace std;
const int N = 2000010;
long long n;
vector<long long> Adj[N];
bool Vis[N];
bool Lucky(long long x) {
  while (x > 0) {
    int d = (x % 10);
    x /= 10;
    if (d != 7 && d != 4) return 0;
  }
  return 1;
}
int Cnt;
void Dfs(int x) {
  Vis[x] = true;
  Cnt++;
  for (int i = 0; i < Adj[x].size(); i++) {
    int to = Adj[x][i];
    if (Vis[to]) continue;
    Dfs(to);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    if (Lucky(w)) continue;
    Adj[a].push_back(b);
    Adj[b].push_back(a);
  }
  vector<int> Components;
  for (int i = 1; i <= n; i++) {
    if (Vis[i]) continue;
    Cnt = 0;
    Dfs(i);
    Components.push_back(Cnt);
  }
  long long Ans = 0;
  for (int i = 0; i < Components.size(); i++) {
    long long Cur = Components[i];
    long long Others = (n - Cur);
    Ans += Cur * (Others * (Others - 1));
  }
  cout << Ans << endl;
  return 0;
}
