#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
template <typename T>
inline void write(T x) {
  long long i = 20;
  char buf[21];
  buf[20] = '\n';
  do {
    buf[--i] = x % 10 + '0';
    x /= 10;
  } while (x);
  do {
    putchar(buf[i]);
  } while (buf[i++] != '\n');
}
void read(long long &x) {
  bool neg = false;
  register long long c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48;
  if (neg) x *= -1;
}
long long n, cnt, vis1[100005], vis2[100005], tot[100005];
vector<long long> v[100005];
bool is_lucky(long long num) {
  while (num) {
    long long x = num % 10;
    num /= 10;
    if (x != 4 and x != 7) return false;
  }
  return true;
}
void dfs1(long long node) {
  cnt++;
  vis1[node] = true;
  for (long long i = 0; i < v[node].size(); i++) {
    long long z = v[node][i];
    if (!vis1[z]) dfs1(z);
  }
}
void dfs2(long long node) {
  vis2[node] = true;
  tot[node] = cnt;
  for (long long i = 0; i < v[node].size(); i++) {
    long long z = v[node][i];
    if (!vis2[z]) dfs2(z);
  }
}
int main() {
  long long i, a, b, c;
  cin >> n;
  for (i = 1; i <= n - 1; i++) {
    cin >> a >> b >> c;
    if (!is_lucky(c)) {
      v[a].push_back(b);
      v[b].push_back(a);
    }
  }
  for (i = 1; i <= n; i++) {
    cnt = 0;
    if (!vis1[i]) {
      dfs1(i);
      dfs2(i);
    }
  }
  long long ans = 0;
  for (i = 1; i <= n; i++) {
    long long out = n - tot[i];
    ans += out * (out - 1);
  }
  cout << ans << endl;
  return 0;
}
