#include <bits/stdc++.h>
using namespace std;
struct Data {
  int y, w;
  Data(int a, int b) : y(a), w(b) {}
};
vector<Data> G1[100010];
int cnt[100010];
bool vis[100010];
int size[100010];
int connect[100010];
bool Check(int n) {
  while (n) {
    if (n % 10 != 4 && n % 10 != 7) return 0;
    n /= 10;
  }
  return 1;
}
void DFS(int u) {
  vis[u] = 1;
  size[u] = 0;
  connect[u] = 0;
  for (int i = 0; i < G1[u].size(); ++i) {
    Data v = G1[u][i];
    if (vis[v.y]) continue;
    DFS(v.y);
    size[u] += size[v.y] + 1;
    if (Check(v.w))
      connect[u] += size[v.y] + 1;
    else
      connect[u] += connect[v.y];
  }
}
void DFSS(int n, int u) {
  vis[u] = 1;
  for (int i = 0; i < G1[u].size(); ++i) {
    Data v = G1[u][i];
    if (vis[v.y]) continue;
    if (Check(v.w))
      cnt[v.y] = (n - size[v.y] - 1) + connect[v.y];
    else
      cnt[v.y] = cnt[u];
    DFSS(n, v.y);
  }
}
void Cal(int n) {
  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (long long)cnt[i] * (cnt[i] - 1);
  }
  printf("%I64d\n", sum);
}
int main() {
  int n;
  scanf("%d", &n);
  int a, b, c;
  for (int i = 1; i < n; ++i) {
    scanf("%d %d %d", &a, &b, &c);
    G1[a].push_back(Data(b, c));
    G1[b].push_back(Data(a, c));
  }
  memset(vis, 0, sizeof(vis));
  DFS(1);
  cnt[1] = connect[1];
  memset(vis, 0, sizeof(vis));
  DFSS(n, 1);
  Cal(n);
  return 0;
}
