#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5 + 5, Mod = 1e9 + 7;
int n, comp;
long long int comp_sz[Maxn], ans;
vector<int> adj[Maxn];
bool mrk[Maxn];
bool lucky(string second) {
  for (int i = 0; i < second.size(); i++) {
    if (second[i] != '4' && second[i] != '7') {
      return 0;
    }
  }
  return 1;
}
void Dfs(int v) {
  comp_sz[comp]++;
  mrk[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i];
    if (!mrk[u]) {
      Dfs(u);
    }
  }
}
void Dfs_All() {
  for (int i = 1; i <= n; i++) {
    if (!mrk[i]) {
      comp = i;
      Dfs(i);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    string w;
    cin >> u >> v >> w;
    if (lucky(w)) continue;
    adj[v].push_back(u), adj[u].push_back(v);
  }
  Dfs_All();
  for (int i = 1; i <= n; i++) {
    ans += comp_sz[i] * (n - comp_sz[i]) * (n - comp_sz[i] - 1);
  }
  cout << ans << '\n';
  return 0;
}
