#include <bits/stdc++.h>
using namespace std;
int n;
struct Node {
  int u, w, next;
};
struct Node node[300010];
int p;
int f[100010], g[100010], num[100010], isgo[100010];
int judge(int n) {
  while (n) {
    if (n % 10 != 4 && n % 10 != 7) return 0;
    n /= 10;
  }
  return 1;
}
void dfs(int u) {
  isgo[u] = 1;
  num[u] = 1;
  f[u] = 0;
  for (int now = node[u].next; now != -1; now = node[now].next) {
    int v = node[now].u, w = node[now].w;
    if (isgo[v] == 1) continue;
    dfs(v);
    if (w == 1)
      f[u] += num[v];
    else
      f[u] += f[v];
    num[u] += num[v];
  }
}
void dp(int u) {
  isgo[u] = 1;
  for (int now = node[u].next; now != -1; now = node[now].next) {
    int v = node[now].u;
    int w = node[now].w;
    if (isgo[v] == 1) continue;
    if (w == 1)
      g[v] = num[1] - num[v];
    else
      g[v] = g[u] + f[u] - f[v];
    dp(v);
  }
}
void add(int u, int v, int w) {
  p++;
  node[p].u = v, node[p].w = w;
  node[p].next = node[u].next;
  node[u].next = p;
}
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) node[i].next = -1;
    p = n;
    for (int i = 1; i < n; i++) {
      int u, v, w;
      scanf("%d %d %d", &u, &v, &w);
      w = judge(w);
      add(u, v, w), add(v, u, w);
    }
    memset(isgo, 0, sizeof(isgo));
    dfs(1);
    memset(isgo, 0, sizeof(isgo));
    g[1] = 0;
    dp(1);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
      ans +=
          1LL * f[i] * (f[i] - 1) + 1LL * g[i] * (g[i] - 1) + 2LL * f[i] * g[i];
    cout << ans << endl;
  }
  return 0;
}
