#include <bits/stdc++.h>
using namespace std;
const int base = 1000000007;
const int maxn = 100100;
pair<int, int> mang[maxn];
int lab[maxn], dem[maxn];
int n, so;
int findset(int i) {
  if (lab[i] == 0)
    return (i);
  else {
    lab[i] = findset(lab[i]);
    return (lab[i]);
  }
}
void xuly() {
  long long kq;
  int x, y, i;
  for (i = 1; i <= so; i++) {
    x = findset(mang[i].first);
    y = findset(mang[i].second);
    if (x != y) {
      lab[y] = x;
    }
  }
  for (i = 1; i <= n; i++) {
    x = findset(i);
    dem[x]++;
  }
  kq = 0;
  for (i = 1; i <= n; i++) {
    x = findset(i);
    kq = kq + (long long)(n - dem[x]) * (long long)(n - dem[x] - 1);
  }
  cout << kq << endl;
}
bool lucky(int u) {
  while (u > 0) {
    if ((u % 10 != 4) && (u % 10 != 7)) {
      return (false);
    }
    u = u / 10;
  }
  return (true);
}
int main() {
  int i, u, v, w;
  scanf("%d", &n);
  for (i = 1; i <= n - 1; i++) {
    scanf("%d%d%d", &u, &v, &w);
    if (lucky(w) == false) {
      so++;
      mang[so] = make_pair(u, v);
    }
  }
  xuly();
}
