#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > v[100010];
long long dp[100010], r[100010], l[100010], n;
void dfs(int x, int p = -1) {
  for (int i = 0; i < v[x].size(); i++) {
    int to = v[x][i].first, le = v[x][i].second;
    if (to == p) continue;
    dfs(to, x);
    dp[x] += dp[to];
  }
  dp[x]++;
}
void f(int x, int p = -1, long long ans = 0) {
  for (int i = 0; i < v[x].size(); i++) {
    int to = v[x][i].first, le = v[x][i].second;
    if (to == p) continue;
    if (le == 1)
      f(to, x, n - dp[to]);
    else
      f(to, x, ans);
    if (le == 1)
      l[x] += dp[to];
    else
      l[x] += l[to];
  }
}
void bfs(int x, int p = -1, int t = 0) {
  int ans = 0;
  r[x] += t;
  for (int i = 0; i < v[x].size(); i++) {
    int to = v[x][i].first, le = v[x][i].second;
    if (to == p) continue;
    if (le == 1)
      bfs(to, x, n - dp[to]);
    else
      bfs(to, x, t + l[x] - l[to]);
  }
}
int main() {
  long long i, x, y, z;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> x >> y >> z;
    int lol = 1;
    while (z != 0) {
      if (z % 10 != 7 && z % 10 != 4) lol = 0;
      z /= 10;
    }
    v[x].push_back({y, lol});
    v[y].push_back({x, lol});
  }
  dfs(1);
  f(1);
  bfs(1);
  long long ans = 0;
  for (i = 1; i <= n; i++) l[i] += r[i];
  for (i = 1; i <= n; i++) ans += (l[i] - 1) * l[i];
  cout << ans;
}
