#include <bits/stdc++.h>
using namespace std;
const int inf = 1e09 + 31;
const long long mxN = 100000;
int n;
int parent[mxN + 1], siz[mxN + 1];
int find_set(int v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (siz[a] < siz[b]) swap(a, b);
    parent[b] = a;
    siz[a] += siz[b];
  }
}
vector<vector<int>> edges;
bool isgood(int a) {
  while (a > 0) {
    if (((a % 10) != 4) && ((a % 10) != 7)) return false;
    a /= 10;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int temp, temp2, temp3;
  cin >> n;
  for (int i = 0; i < n; i++) {
    parent[i] = i;
    siz[i] = 1;
  }
  edges.resize(n, vector<int>(0));
  for (int i = 0; i < n - 1; i++) {
    cin >> temp >> temp2 >> temp3;
    temp--, temp2--;
    if (!isgood(temp3)) {
      union_sets(temp, temp2);
    }
  }
  long long out = 0;
  for (int i = 0; i < n; i++) {
    long long bl = siz[find_set(i)] - 1;
    bl = n - bl - 1;
    out += bl * (bl - 1);
  }
  cout << out;
}
