#include <bits/stdc++.h>
using namespace std;
long long int dp[100005];
long long int dp2[100005];
map<pair<long long int, long long int>, long long int> cn;
vector<pair<long long int, long long int> > v[100005];
bool vis[100005];
long long int sb[100005];
long long int n;
long long int fans = 0;
void initi() {
  for (long long int i = 0; i < 100005; i++) {
    sb[i] = 0;
    dp2[i] = 0;
    dp[i] = 0;
    vis[i] = false;
    v[i].clear();
  }
}
inline void dfs0(long long int node, long long int par) {
  long long int cnt = 1;
  for (auto it : v[node]) {
    if (it.first != par) {
      dfs0(it.first, node);
      cnt += sb[it.first];
    }
  }
  sb[node] = cnt;
}
inline void dfs(long long int node) {
  long long int ans = 0;
  vis[node] = true;
  for (auto it : v[node]) {
    if (!vis[it.first]) {
      dfs(it.first);
      if (it.second == 1) {
        ans += sb[it.first];
      } else {
        ans += dp[it.first];
      }
    }
  }
  dp[node] = ans;
}
inline void dfs2(long long int node, long long int par) {
  long long int ans = 0;
  if (par != 0) {
    if (cn[{par, node}] == 1) {
      ans += sb[par];
      ans -= sb[node];
    } else {
      ans += (dp[par] - dp[node]);
    }
  }
  if (par != 0 && cn[{par, node}] == 1) {
    ans += sb[1] - sb[par];
  } else {
    ans += dp2[par];
  }
  dp2[node] = ans;
  for (auto it : v[node]) {
    if (it.first != par) {
      dfs2(it.first, node);
    }
  }
}
inline void solve() {
  initi();
  cin >> n;
  for (long long int i = 1; i < n; i++) {
    long long int x, y, z;
    cin >> x >> y >> z;
    long long int fl = 1;
    while (z != 0) {
      if (z % 10 != 7 && z % 10 != 4) {
        fl = 0;
        break;
      }
      z = z / 10;
    }
    v[x].push_back({y, fl});
    v[y].push_back({x, fl});
    cn[{x, y}] = fl;
    cn[{y, x}] = fl;
  }
  dfs0(1, 0);
  dfs(1);
  dfs2(1, 0);
  fans = 0;
  for (long long int i = 1; i <= n; i++) {
    fans += max(0LL, dp[i] * (dp[i] - 1));
    fans += max(0LL, dp2[i] * (dp2[i] - 1));
    fans += 2 * max(0LL, dp[i] * dp2[i]);
  }
  cout << fans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tests = 1;
  while (tests--) {
    solve();
  }
}
