#include <bits/stdc++.h>
using namespace std;
const int MX = 200005;
int N;
vector<vector<pair<int, int> > > G(MX);
int p[MX];
long long sz[MX];
int f(int x) {
  if (x == p[x]) return x;
  return p[x] = f(p[x]);
}
void un(int x, int y) {
  x = f(x);
  y = f(y);
  if (x == y) return;
  p[x] = y;
  sz[y] += sz[x];
}
void dfs(int x, int p = -1) {
  for (int i = 0; i < G[x].size(); i++) {
    int ch = G[x][i].first, l = G[x][i].second;
    if (ch == p) continue;
    dfs(ch, x);
    if (l) un(ch, x);
  }
}
bool luc(int n) {
  while (n) {
    if (n % 10 != 7 && n % 10 != 4) return 1;
    n /= 10;
  }
  return 0;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    p[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < N - 1; i++) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    int l = luc(w);
    G[a].push_back(make_pair(b, l));
    G[b].push_back(make_pair(a, l));
  }
  dfs(1);
  long long an = 0;
  for (int i = 1; i <= N; i++) {
    int p = f(i);
    long long s = sz[p];
    long long o = N - s;
    an += o * (o - 1);
  }
  cout << an << "\n";
  return 0;
}
