#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 5;
vector<pair<int, bool> > adj[MAXN];
int n;
int child[MAXN], dp[MAXN], dp2[MAXN];
bool check(int a) {
  bool b = true;
  while (a > 0) {
    if ((a % 10) != 4 && (a % 10) != 7) b = false;
    a /= 10;
  }
  return b;
}
void dfs(int v, int fat) {
  child[v] = 1;
  for (int i = 0; i < (int)adj[v].size(); i++) {
    int u = adj[v][i].first;
    bool b = adj[v][i].second;
    if (u == fat) continue;
    dfs(u, v);
    child[v] += child[u];
    if (b)
      dp[v] += child[u];
    else
      dp[v] += dp[u];
  }
}
void dfs2(int v, int fat, bool b) {
  if (fat != 0) {
    if (b)
      dp2[v] += n - child[v];
    else
      dp2[v] += dp2[fat] + dp[fat] - dp[v];
  }
  for (int i = 0; i < (int)adj[v].size(); i++) {
    int u = adj[v][i].first;
    if (u == fat) continue;
    dfs2(u, v, adj[v][i].second);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    bool q = false;
    if (check(c)) q = true;
    adj[a].push_back(pair<int, bool>(b, q));
    adj[b].push_back(pair<int, bool>(a, q));
  }
  dfs(1, 0);
  dfs2(1, 0, 0);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (long long)dp[i] * (dp[i] - 1) + (long long)dp2[i] * (dp2[i] - 1) +
           2ll * dp[i] * dp2[i];
  }
  cout << ans << endl;
  return 0;
}
