#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const int MAXN = 100000;
int p[MAXN], sz[MAXN];
int lucky(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int pred(int v) {
  if (v == p[v]) return v;
  return p[v] = pred(p[v]);
}
int main() {
  int n, a, b, c;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    p[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d%d", &a, &b, &c);
    --a;
    --b;
    if (!lucky(c)) {
      a = pred(a);
      b = pred(b);
      if (a != b) {
        if (sz[a] < sz[b]) swap(a, b);
        p[b] = a;
        sz[a] += sz[b];
      }
    }
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    int cur = pred(i);
    res = res + (long long)(n - sz[cur]) * (n - sz[cur] - 1);
  }
  cout << res << endl;
  return 0;
}
