#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100007];
int cmp[100007], belong[100007];
bool vis[100007];
int cnt, cc;
void dfs(int u) {
  cnt++;
  belong[u] = cc;
  vis[u] = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (vis[v]) continue;
    dfs(v);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int p, q, w;
    scanf("%d %d %d", &p, &q, &w);
    int tmp = w;
    bool f = 0;
    while (tmp) {
      int m = tmp % 10;
      tmp /= 10;
      if (m != 4 and m != 7) {
        f = 1;
        break;
      }
    }
    if (f) {
      adj[p].push_back(q);
      adj[q].push_back(p);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    cnt = 0;
    cc++;
    dfs(i);
    cmp[cc] = cnt;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long rem = n - cmp[belong[i]];
    ans += rem * (rem - 1);
  }
  cout << ans << endl;
}
