#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5;
vector<int> nb[MAXn];
bool islucky(int x) {
  while (x > 0) {
    int r = x % 10;
    if (r != 4 && r != 7) return false;
    x /= 10;
  }
  return true;
}
bool mark[MAXn];
int dfs(int v) {
  mark[v] = true;
  int ret = 1;
  for (int i = 0; i < (int)nb[v].size(); i++)
    if (!mark[nb[v][i]]) ret += dfs(nb[v][i]);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int v1, v2, w;
    cin >> v1 >> v2 >> w;
    v1--;
    v2--;
    if (!islucky(w)) {
      nb[v1].push_back(v2);
      nb[v2].push_back(v1);
    }
  }
  long long count = 0;
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      int c = dfs(i);
      count += c * ((long long)(n - c) * (n - c) - (n - c));
    }
  cout << count << endl;
  return 0;
}
