#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
vector<int> g[MAXN];
bool mark[MAXN];
int dfs(int v) {
  mark[v] = true;
  int res = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (!mark[u]) res += dfs(u);
  }
  return res;
}
bool lucky(int x) {
  while (x) {
    if (x % 10 != 7 and x % 10 != 4) return false;
    x /= 10;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    if (!lucky(w)) {
      g[v].push_back(u);
      g[u].push_back(v);
    }
  }
  long long ans = 0, k;
  for (int i = 1; i <= n; i++) {
    if (mark[i]) continue;
    long long x = dfs(i);
    k = n - x;
    ans += x * k * (k - 1);
  }
  cout << ans << endl;
  return 0;
}
