#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> adj[100009];
int dp[100009];
int sz[100009];
long long int val[100009];
long long int ans = 0;
int n;
bool f(int x) {
  while (x != 0) {
    if (x % 10 != 7 and x % 10 != 4) return false;
    x = x / 10;
  }
  return true;
}
long long int calc(int x) {
  return (long long int)(x) * ((long long int)x) - x;
}
void dfs(int i, int p) {
  sz[i] = 1;
  for (auto x : adj[i])
    if (x.first != p) {
      dfs(x.first, i);
      sz[i] += sz[x.first];
      if (f(x.second))
        dp[i] += sz[x.first];
      else
        dp[i] += dp[x.first];
    }
}
void reroot(int i, int p, int pr) {
  val[i] = (calc(dp[i] + pr));
  ans += val[i];
  for (auto x : adj[i])
    if (x.first != p) {
      if (f(x.second))
        reroot(x.first, i, n - sz[x.first]);
      else
        reroot(x.first, i, pr + dp[i] - dp[x.first]);
    }
}
int main() {
  cin >> n;
  int u, v, w;
  for (int i = 1; i < n; i++) {
    cin >> u >> v >> w;
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
  }
  dfs(1, 1);
  reroot(1, 1, 0);
  cout << ans;
}
