#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
long long n, x, y, w;
long long javab;
vector<int> a[maxn];
bool mark[maxn];
long long ans[maxn], counter;
vector<int> tmp;
bool lucky(int b) {
  bool c = 1;
  while (b) {
    if (b % 10 != 4 && b % 10 != 7) c = 0;
    b /= 10;
  }
  return c;
}
void dfs(int g) {
  mark[g] = 1;
  counter++;
  tmp.push_back(g);
  for (int i = 0; i < int(a[g].size()); i++)
    if (!mark[a[g][i]]) dfs(a[g][i]);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) ans[i] = n;
  for (int i = 1; i < n; i++) {
    cin >> x >> y >> w;
    if (lucky(w) == 0) {
      a[x].push_back(y);
      a[y].push_back(x);
    }
  }
  for (int i = 1; i <= n; i++)
    if (mark[i] == 0) {
      counter = 0;
      dfs(i);
      for (int j = 0; j < int(tmp.size()); j++) ans[tmp[j]] -= counter;
      tmp.clear();
    }
  for (int i = 1; i <= n; i++) javab += ans[i] * (ans[i] - 1);
  cout << javab << endl;
  return 0;
}
