#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
const int M = 505;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
vector<int> dsu;
long long sz[N];
int getpar(int a) { return (a == dsu[a]) ? a : dsu[a] = getpar(dsu[a]); }
void Merge(int a, int b) {
  a = getpar(a);
  b = getpar(b);
  sz[a] += sz[b];
  dsu[b] = a;
  sz[b] = 0;
}
bool good(int edge) {
  while (edge) {
    int a = edge % 10;
    edge /= 10;
    if (a == 7 || a == 4) continue;
    return 0;
  }
  return 1;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) dsu.push_back(i), sz[i] = 1LL;
  for (int i = 1; i < n; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (!good(c)) {
      Merge(a, b);
    }
  }
  if (n < 3) {
    cout << 0;
    return 0;
  }
  long long sol = 1LL * n * (n - 1LL) * (n - 2LL);
  for (int i = 1; i <= n; i++) {
    int a = getpar(i);
    if (sz[a] < 2) continue;
    sol -= (sz[a] * (sz[a] - 1LL) * (n - 2) * 2LL);
    sol += (sz[a] * (sz[a] - 1LL) * (sz[a] - 2LL));
    sz[a] = 0;
  }
  cout << sol;
  return 0;
}
