#include <bits/stdc++.h>
using namespace std;
int N, M;
long long comp[100005], ata[100005];
vector<pair<long long, pair<int, int> > > edge;
vector<pair<int, int> > V[100005];
int atabul(int x) {
  if (ata[x] == x) return x;
  return ata[x] = atabul(ata[x]);
}
bool lucky(long long x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  long long res = 0;
  long long x, y, t, k;
  ios ::sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; i++) ata[i] = i;
  for (int i = 1; i <= N - 1; i++) {
    cin >> x >> y >> t;
    V[x].push_back(make_pair(y, t));
    V[y].push_back(make_pair(x, t));
    edge.push_back(make_pair(t, make_pair(x, y)));
  }
  for (int i = 0; i <= N - 2; i++) {
    if (!lucky(edge[i].first))
      ata[atabul(edge[i].second.first)] = atabul(edge[i].second.second);
  }
  for (int i = 1; i <= N; i++) comp[atabul(i)]++;
  for (int i = 1; i <= N; i++) {
    if (!comp[i]) continue;
    k = N - comp[i];
    res += comp[i] * k * (k - 1);
  }
  cout << res << endl;
  cerr << "\nTime: " << (double)clock() / CLOCKS_PER_SEC << " seconds." << endl;
  return 0;
}
