#include <bits/stdc++.h>
using namespace std;
bool is_lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
vector<int> adia[100010];
bool viz[100010];
int col[100010], cntcol;
int frc[100010];
void dfs(int nod, int c = 0) {
  if (!c) c = ++cntcol;
  frc[c]++;
  col[nod] = c;
  viz[nod] = 1;
  for (auto i : adia[nod])
    if (!viz[i]) dfs(i, c);
}
int main() {
  int n, m, a, b, c;
  cin >> n;
  for (int i(1); i < n; i++) {
    cin >> a >> b >> c;
    if (!is_lucky(c)) {
      adia[a].push_back(b);
      adia[b].push_back(a);
    }
  }
  for (int i(1); i <= n; i++)
    if (!viz[i]) dfs(i);
  long long ans(0);
  for (int i(1); i <= n; i++) {
    int rms = n - frc[col[i]];
    ans += 1ll * rms * (rms - 1);
  }
  cout << ans;
  return 0;
}
