#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1 << (long long)62, mod = 1000000007;
const int SIZE = 100001;
vector<int> data(SIZE, -1);
long long ct[SIZE];
bool islucky(int sum) {
  if (sum == 0) return false;
  while (true) {
    if (sum == 0) break;
    if (sum % 10 != 7 && sum % 10 != 4) return false;
    sum /= 10;
  }
  return true;
}
int find_root(int a) {
  return ((data[a] < 0) ? a : (data[a] = find_root(data[a])));
}
bool link(int a, int b) {
  int ra = find_root(a);
  int rb = find_root(b);
  if (ra != rb) {
    if (data[rb] < data[ra]) swap(ra, rb);
    data[ra] += data[rb];
    data[rb] = ra;
  }
  return (ra != rb);
}
int main() {
  long long n;
  while (cin >> n) {
    for (int i = 0; i < SIZE; i++) {
      data[i] = -1;
      ct[i] = 0;
    }
    for (int i = 0; i < n - 1; i++) {
      int a, b, w;
      cin >> a >> b >> w;
      bool lucky = islucky(w);
      if (lucky) continue;
      link(a, b);
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int ra = find_root(i + 1);
      ct[ra]++;
    }
    for (int i = 0; i < n; i++) {
      if (ct[i + 1] == 0) continue;
      long long k = n - ct[i + 1];
      ans += ((k * (k - 1)) * ct[i + 1]);
    }
    cout << ans << endl;
  }
  return 0;
}
