#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
int n;
vector<vector<int> > g;
bool vis[sz];
int dfs(int u) {
  vis[u] = true;
  int x = 1;
  for (int v : g[u])
    if (!vis[v]) x += dfs(v);
  return x;
}
int main() {
  scanf("%d", &n);
  g.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    --u, --v;
    bool lucky = true;
    while (w > 0 && lucky) {
      int r = w % 10;
      if (r != 4 && r != 7) lucky = false;
      w /= 10;
    }
    if (!lucky) {
      g[u].push_back(v);
      g[v].push_back(u);
    }
  }
  long long tot = 0;
  for (int i = 0; i < n; ++i) vis[i] = false;
  for (int i = 0; i < n; ++i)
    if (!vis[i]) {
      long long x = dfs(i);
      tot += x * (n - x) * (n - x - 1);
    }
  cout << tot << endl;
}
