#include <bits/stdc++.h>
const int N = 1e5;
using namespace std;
struct edges {
  int to, cost;
} edge[N * 2 + 5];
int n, nxt[N * 2 + 5], head[N + 5], edge_cnt, size[N + 5];
long long f[N + 5], ans;
void add_edge(int u, int v, int w) {
  edge[++edge_cnt] = (edges){v, w};
  nxt[edge_cnt] = head[u];
  head[u] = edge_cnt;
}
void dfs1(int u, int fa) {
  size[u] = 1;
  for (int i = head[u]; i; i = nxt[i]) {
    int v = edge[i].to, w = edge[i].cost;
    if (v == fa) continue;
    dfs1(v, u);
    size[u] += size[v];
    if (w)
      f[u] += size[v];
    else
      f[u] += f[v];
  }
}
void dfs2(int u, int fa) {
  for (int i = head[u]; i; i = nxt[i]) {
    int v = edge[i].to, w = edge[i].cost;
    if (v == fa) continue;
    if (w)
      f[v] += size[u] - size[v];
    else
      f[v] += f[u] - f[v];
    size[v] += size[u] - size[v];
    dfs2(v, u);
  }
}
int main() {
  scanf("%d", &n);
  int u, v, w;
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &u, &v, &w);
    int fl = 1;
    while (w) {
      if (w % 10 != 4 && w % 10 != 7) fl = 0;
      w /= 10;
    }
    add_edge(u, v, fl);
    add_edge(v, u, fl);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) ans += 1ll * f[i] * (f[i] - 1);
  cout << ans << endl;
  return 0;
}
