#include <bits/stdc++.h>
using namespace std;
set<long long> nums;
vector<vector<pair<int, int> > > G;
vector<int> mark;
int dfs(int v) {
  if (mark[v]) {
    return 0;
  }
  int sz = 1;
  mark[v] = 1;
  for (auto x : G[v]) {
    int u = x.first;
    if (x.second == 0) {
      sz += dfs(u);
    }
  }
  return sz;
}
int main() {
  for (int i = 1; i < 11; i++) {
    for (int mask = 0; mask < (1 << i); mask++) {
      long long cur = 0;
      for (int j = 0; j < i; j++) {
        if ((mask >> j) & 1) {
          cur = cur * 10 + 7;
        } else {
          cur = cur * 10 + 4;
        }
      }
      nums.insert(cur);
    }
  }
  int n;
  cin >> n;
  G.resize(n + 1);
  mark.resize(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    w = (nums.find(w) != nums.end());
    G[v].push_back({u, w});
    G[u].push_back({v, w});
  }
  vector<long long> a(n + 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = dfs(i);
    ans += a[i] * (n - a[i]) * (n - a[i] - 1);
  }
  cout << ans << endl;
  return 0;
}
