#include <bits/stdc++.h>
using namespace std;
long long n, u, v, c[100001], cnt[100001], ans = 0;
string w;
bool mark[100001], z;
vector<pair<int, bool> > adj[100001];
vector<int> com;
void dfs(int x) {
  mark[x] = 1;
  com.push_back(x);
  for (int i = 0; i < adj[x].size(); i++) {
    int y = adj[x][i].first;
    if (!mark[y] && !adj[x][i].second) {
      dfs(y);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    z = 1;
    u--;
    v--;
    for (int j = 0; j < w.size(); j++)
      if (w[j] != '7' && w[j] != '4') {
        z = 0;
        break;
      }
    adj[u].push_back({v, z});
    adj[v].push_back({u, z});
  }
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      com.clear();
      dfs(i);
      cnt[i] = (n - (com.size()));
      ans += ((cnt[i] - 1) * cnt[i] * com.size());
    }
  }
  cout << ans;
}
