#include <bits/stdc++.h>
using namespace std;
const long long sz = 1e5 + 5;
long long nodesUp[sz], nodesDown[sz], vis[sz], ss[sz], n;
vector<pair<long long, long long> > adjlist[sz];
vector<long long> clist[sz];
pair<long long, long long> par[sz];
bool lucky(long long num) {
  while (num) {
    if (num % 10 != 4 && num % 10 != 7) {
      return 0;
    }
    num /= 10;
  }
  return 1;
}
void dfs(long long node, long long parent, long long weight) {
  ss[node] = 1;
  vis[node] = 1;
  par[node].first = parent;
  par[node].second = weight;
  for (auto edge : adjlist[node]) {
    if (!vis[edge.first]) {
      clist[node].push_back(edge.first);
      dfs(edge.first, node, edge.second);
      ss[node] += ss[edge.first];
      if (edge.second)
        nodesDown[node] += ss[edge.first];
      else
        nodesDown[node] += nodesDown[edge.first];
    }
  }
}
void dfs2(long long node) {
  if (par[node].second == 1)
    nodesUp[node] = n - ss[node];
  else if (node != 1) {
    nodesUp[node] += nodesDown[par[node].first] - nodesDown[node];
    nodesUp[node] += nodesUp[par[node].first];
  }
  for (auto child : clist[node]) {
    dfs2(child);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n - 1; ++i) {
    long long x, y, w;
    cin >> x >> y >> w;
    adjlist[x].push_back(make_pair(y, lucky(w)));
    adjlist[y].push_back(make_pair(x, lucky(w)));
  }
  dfs(1, 0, 0);
  dfs2(1);
  long long ans = 0;
  for (long long i = 1; i < n + 1; ++i) {
    long long k = (nodesUp[i] + nodesDown[i]);
    ans += k * (k - 1);
  }
  cout << ans;
}
