#include <bits/stdc++.h>
using namespace std;
long long ans;
int n, countr[200000], above[200000], below[200000];
std::vector<int> adj[200000], wt[200000];
int lucky(long long w) {
  while (w) {
    if (w % 10 != 4 && w % 10 != 7) return 0;
    w /= 10;
  }
  return 1;
}
void dfs(int curr, int prev) {
  for (int i = 0; i < adj[curr].size(); i++) {
    if (adj[curr][i] == prev) continue;
    dfs(adj[curr][i], curr);
    countr[curr] += countr[adj[curr][i]];
    if (wt[curr][i])
      below[curr] += countr[adj[curr][i]];
    else
      below[curr] += below[adj[curr][i]];
  }
  countr[curr]++;
}
void dfs1(int curr, int prev) {
  for (int i = 0; i < adj[curr].size(); i++) {
    if (adj[curr][i] == prev) continue;
    if (wt[curr][i])
      above[adj[curr][i]] = n - countr[adj[curr][i]];
    else {
      above[adj[curr][i]] = below[curr] - below[adj[curr][i]] + above[curr];
    }
    dfs1(adj[curr][i], curr);
  }
}
int main() {
  int i, temp1, temp2;
  long long temp3;
  scanf("%d", &n);
  for (i = 0; i < n - 1; i++) {
    scanf("%d", &temp1);
    scanf("%d", &temp2);
    scanf("%I64d", &temp3);
    adj[temp1].push_back(temp2);
    adj[temp2].push_back(temp1);
    temp3 = lucky(temp3);
    wt[temp1].push_back(temp3);
    wt[temp2].push_back(temp3);
  }
  dfs(1, -1);
  dfs1(1, -1);
  for (i = 1; i <= n; i++)
    ans += ((long long)(above[i] + below[i])) *
           ((long long)(above[i] + below[i] - 1LL));
  printf("%I64d\n", ans);
}
