#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 5;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
long long sub[mxn];
int par[mxn];
int Find(int p) {
  if (p == par[p]) return p;
  return par[p] = Find(par[p]);
}
void merge(int u, int v) {
  u = Find(u);
  v = Find(v);
  if (u == v) return;
  sub[u] += sub[v];
  par[v] = u;
}
int check(int x) {
  while (x) {
    int y = x % 10;
    if (y != 4 && y != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) par[i] = i, sub[i] = 1;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (check(w)) continue;
    merge(u, v);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long p = Find(i);
    long long x = n - sub[p];
    long long tt = (x * (x - 1));
    ans += tt;
  }
  cout << ans << '\n';
  return 0;
}
