#include <bits/stdc++.h>
using namespace std;
long long t, mark[100009], ans, deg[100009], n, x, y, z;
vector<int> g[100009];
long long check(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
int moalefe(int x) {
  t++;
  mark[x] = 1;
  for (int i = 0; i < g[x].size(); i++) {
    if (mark[g[x][i]] == 0) {
      moalefe(g[x][i]);
    }
  }
  return 0;
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y >> z;
    if (check(z) == 0) {
      g[x].push_back(y);
      deg[y]++;
      g[y].push_back(x);
      deg[x]++;
    }
  }
  for (int i = 1; i < n + 1; i++) {
    if (mark[i] == 0 && deg[i] >= 1) {
      t = 0;
      moalefe(i);
      ans += ((t * (t - 1)) / 2) * 4 * (n - t);
      if (t > 2) {
        ans += (t * (t - 1) * (t - 2));
      }
    }
  }
  cout << (n * (n - 1) * (n - 2)) - ans;
}
