#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long sum, ways[N];
int n, parent[N], kids[N], dis[N], max_dis;
vector<pair<int, int> > adj[N];
vector<int> level[N];
bool seen[N];
bool lucky(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
void dfs(int v) {
  seen[v] = true;
  for (int i = 0; i < adj[v].size(); i++)
    if (!seen[adj[v][i].first]) {
      parent[adj[v][i].first] = v;
      dis[adj[v][i].first] = dis[v] + 1;
      level[dis[v] + 1].push_back(adj[v][i].first);
      max_dis = max(max_dis, dis[v] + 1);
      dfs(adj[v][i].first);
    }
  return;
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    pair<int, int> tmp;
    int v;
    cin >> v >> tmp.first >> tmp.second;
    tmp.first--;
    v--;
    adj[v].push_back(tmp);
    swap(tmp.first, v);
    adj[v].push_back(tmp);
  }
  parent[0] = 0;
  level[0].push_back(0);
  dfs(0);
  for (int i = max_dis; i >= 0; i--)
    for (int j = 0; j < level[i].size(); j++)
      for (int k = 0; k < adj[level[i][j]].size(); k++)
        if (parent[level[i][j]] != adj[level[i][j]][k].first) {
          kids[level[i][j]] += kids[adj[level[i][j]][k].first] + 1;
          if (lucky(adj[level[i][j]][k].second))
            ways[level[i][j]] += kids[adj[level[i][j]][k].first] + 1;
          else
            ways[level[i][j]] += ways[adj[level[i][j]][k].first];
        }
  sum += ways[0] * (ways[0] - 1);
  for (int i = 0; i <= max_dis; i++)
    for (int j = 0; j < level[i].size(); j++)
      for (int k = 0; k < adj[level[i][j]].size(); k++)
        if (parent[level[i][j]] != adj[level[i][j]][k].first) {
          if (lucky(adj[level[i][j]][k].second))
            ways[adj[level[i][j]][k].first] +=
                (n - kids[adj[level[i][j]][k].first] - 1);
          else
            ways[adj[level[i][j]][k].first] = ways[level[i][j]];
          sum += ways[adj[level[i][j]][k].first] *
                 (ways[adj[level[i][j]][k].first] - 1);
        }
  cout << sum;
  return 0;
}
