#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 200003, mod = 1e9 + 7;
const long long inf = (1e18 + 11);
int n, par[N], sz[N];
bool lucky(int x) {
  int y;
  while (x) {
    y = x % 10;
    if (y != 4 and y != 7) break;
    x /= 10;
  }
  return (x == 0);
}
int root(int x) {
  while (par[x] != x) {
    par[x] = par[par[x]];
    x = par[x];
  }
  return x;
}
void unite(int x, int y) {
  int rx = root(x), ry = root(y);
  if (rx == ry) return;
  if (sz[rx] < sz[ry]) swap(rx, ry);
  par[ry] = rx;
  sz[rx] += sz[ry];
  sz[ry] = 0;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int i, j, u, v, w;
  for (i = 1; i <= n; ++i) sz[i] = 1, par[i] = i;
  for (i = 1; i <= n - 1; ++i) {
    cin >> u >> v >> w;
    if (!lucky(w)) unite(u, v);
  }
  long long ans = 0;
  for (i = 1; i <= n; ++i) ans += (sz[i] * 1ll * (n - sz[i]) * (n - sz[i] - 1));
  cout << ans;
  return 0;
}
