#include <bits/stdc++.h>
using namespace std;
long long n;
long long arr[100005];
long long sz[100005];
bool is_lucky(long long a) {
  if (a == 0) {
    return true;
  } else {
    long long x = a % 10;
    bool t = false;
    if (x == 4 || x == 7) {
      t = true;
    }
    return t && is_lucky(a / 10);
  }
}
void init() {
  for (int i = 0; i <= n; i++) {
    arr[i] = i;
    sz[i] = 1;
  }
  return;
}
long long find(long long a) {
  if (arr[a] != a) {
    arr[a] = find(arr[a]);
  }
  return arr[a];
}
void un(long long a, long long b) {
  long long root_a = find(a);
  long long root_b = find(b);
  if (sz[root_a] != sz[root_b]) {
    arr[root_b] = root_a;
    sz[root_a] += sz[root_b];
  } else {
    arr[root_a] = root_b;
    sz[root_b] += sz[root_a];
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  init();
  if (n < 3) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (!is_lucky(c)) {
      un(a, b);
    }
  }
  long long ans = n * (n - 1) * (n - 2);
  set<long long> s;
  for (int i = 1; i <= n; i++) {
    s.insert(find(i));
  }
  for (auto u : s) {
    long long tmp = sz[u];
    if (tmp >= 2) {
      ans -= tmp * (tmp - 1) * (tmp - 2);
    }
    long long hano = tmp * (tmp - 1) / 2;
    ans -= 4 * hano * (n - tmp);
  }
  cout << ans << endl;
  return 0;
}
