#include <bits/stdc++.h>
using namespace std;
long long ans, c[100200];
vector<pair<int, int> > g[100200];
int n, f[100200], cc;
bool lucky(int x) {
  while (x) {
    if (!(x % 10 == 4 || x % 10 == 7)) return false;
    x /= 10;
  }
  return true;
}
long long A(long long x) {
  if (x < 2) return 0;
  return x * (x - 1);
}
void dfs(int now, int fa) {
  int u;
  for (int i = 0; i < g[now].size(); i++)
    if ((u = g[now][i].first) != fa) {
      if (g[now][i].second == 1)
        f[u] = ++cc;
      else
        f[u] = f[now];
      dfs(u, now);
    }
}
int main() {
  int x, y, w;
  cin >> n;
  for (int i = 1; i < n; i++) {
    cin >> x >> y >> w;
    if (lucky(w))
      w = 1;
    else
      w = 0;
    g[x].push_back(make_pair(y, w));
    g[y].push_back(make_pair(x, w));
  }
  memset(c, 0, sizeof(c));
  f[1] = cc = 1;
  dfs(1, 0);
  for (int i = 1; i <= n; i++) c[f[i]]++;
  ans = 0;
  for (int i = 1; i <= cc; i++) {
    ans += A(n - c[i]) * c[i];
  }
  cout << ans << endl;
  return 0;
}
