#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int head[MAXN], X[MAXN], Y[MAXN], C[MAXN], scon[MAXN], snon[MAXN];
int N;
long long res;
bool visited[MAXN];
pair<int, int> adj[MAXN * 2];
int ktr(int N) {
  while (N > 0) {
    int t = N % 10;
    if (t != 4 && t != 7) return 0;
    N /= 10;
  }
  return 1;
}
void nhap() {
  scanf("%d", &N);
  memset(head, 0, sizeof(head));
  int t;
  for (int i = 1; i < N; i++) {
    scanf("%d%d%d", &X[i], &Y[i], &t);
    head[X[i]]++;
    head[Y[i]]++;
    C[i] = ktr(t);
  }
  for (int i = 2; i <= N + 1; i++) head[i] += head[i - 1];
  for (int i = 1; i < N; i++) {
    adj[head[X[i]]--] = make_pair(Y[i], C[i]);
    adj[head[Y[i]]--] = make_pair(X[i], C[i]);
  }
}
void dfs(int u) {
  scon[u] = 1;
  snon[u] = 0;
  for (int j = head[u] + 1; j <= head[u + 1]; j++) {
    int v = adj[j].first;
    if (visited[v]) continue;
    visited[v] = 1;
    dfs(v);
    scon[u] += scon[v];
    if (adj[j].second)
      snon[u] += scon[v];
    else
      snon[u] += snon[v];
  }
}
void dfv(int u, int p) {
  int t = p + snon[u];
  res += (t * 1ll * (t - 1));
  for (int j = head[u] + 1; j <= head[u + 1]; j++) {
    int v = adj[j].first;
    if (visited[v]) continue;
    visited[v] = 1;
    if (adj[j].second) {
      dfv(v, N - scon[v]);
    } else {
      dfv(v, p + snon[u] - snon[v]);
    }
  }
}
void tinh() {
  res = 0;
  memset(visited, 0, sizeof(visited));
  visited[1] = 1;
  dfs(1);
  memset(visited, 0, sizeof(visited));
  visited[1] = 1;
  dfv(1, 0);
  cout << res << endl;
}
int main() {
  nhap();
  tinh();
  fclose(stdin);
  fclose(stdout);
  return 0;
}
