#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int h[N] = {0}, b[N], n, m, x, y, w;
vector<int> a[N];
bool lucky(int x) {
  while (x) {
    if ((x % 10) != 4 && (x % 10) != 7) return false;
    x /= 10;
  }
  return true;
}
void prepare() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d%d", &x, &y, &w);
    if (!lucky(w)) {
      a[x].push_back(y);
      a[y].push_back(x);
    }
  }
}
void BFS(int x) {
  queue<int> q;
  m++;
  int c = 0;
  q.push(x);
  h[x] = 1;
  while (!q.empty()) {
    int cx = q.front();
    q.pop();
    c++;
    for (vector<int>::iterator i = a[cx].begin(); i < a[cx].end(); i++)
      if (h[*i] == 0) h[*i] = 1, q.push(*i);
  }
  b[m] = c;
}
void solve() {
  for (int i = 1; i <= n; i++)
    if (h[i] == 0) BFS(i);
  long long ans = 0;
  for (int i = 1; i <= m; i++) ans += 1LL * (n - b[i]) * (n - b[i] - 1) * b[i];
  cout << ans;
}
int main() {
  prepare();
  solve();
}
