#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 private:
  vector<int> par;
  vector<int> rank;

 public:
  UnionFind(int n) {
    par.reserve(n);
    rank.reserve(n);
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y]) rank[x]++;
    }
  }
  inline bool same(int x, int y) { return find(x) == find(y); }
};
vector<pair<int, int> > G[100000];
int islucky(int v) {
  while (v) {
    if (v % 10 == 7 || v % 10 == 4) {
      v /= 10;
    } else {
      return false;
    }
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  UnionFind uf(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (islucky(c) == false) {
      uf.unite(a, b);
    }
  }
  map<int, int> cnt;
  for (int i = 0; i < n; i++) {
    cnt[uf.find(i)]++;
  }
  long long int res = 0;
  for (__typeof(cnt.begin()) it = cnt.begin(); it != cnt.end(); it++) {
    res += (long long int)it->second * (long long int)(n - it->second) *
           (long long int)(n - it->second - 1);
  }
  cout << res << endl;
  return 0;
}
