#include <bits/stdc++.h>
using namespace std;
int check(int x) {
  while (x != 0) {
    if (x % 10 != 4 && x % 10 != 7) {
      return 0;
    }
    x /= 10;
  }
  return 1;
}
vector<vector<pair<int, int> > > graph(100004);
pair<int, int> down[100004];
int top[100004];
int vis[100004];
int vis1[100004];
int n;
pair<int, int> dfs(int s) {
  vis[s] = 1;
  pair<int, int> val = make_pair(1, 0);
  for (int i = 0; i < graph[s].size(); i++) {
    if (vis[graph[s][i].first] == 0) {
      pair<int, int> temp = dfs(graph[s][i].first);
      if (graph[s][i].second == 1) {
        val.second += temp.first;
      } else {
        val.second += temp.second;
      }
      val.first += temp.first;
    }
  }
  down[s] = val;
  return val;
}
void dfs1(int s, int p, int flag) {
  vis1[s] = 1;
  if (flag == 1) {
    top[s] = n - down[s].first;
  } else {
    top[s] = top[p] + down[p].second - down[s].second;
  }
  for (int i = 0; i < graph[s].size(); i++) {
    if (vis1[graph[s][i].first] == 0) {
      if (graph[s][i].second == 1) {
        dfs1(graph[s][i].first, s, 1);
      } else {
        dfs1(graph[s][i].first, s, 0);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int i, j, k, x, y, t, m;
  cin >> n;
  for (i = 0; i < n - 1; i++) {
    cin >> x >> y >> m;
    int val = check(m);
    graph[x].push_back(make_pair(y, val));
    graph[y].push_back(make_pair(x, val));
  }
  dfs(1);
  dfs1(1, 1, 0);
  long long ans = 0;
  for (i = 1; i <= n; i++) {
    long long temp = top[i] + down[i].second;
    ans += temp * (temp - 1);
  }
  cout << ans << endl;
  return 0;
}
