#include <bits/stdc++.h>
const long long INF = (long long)1e11 + 1;
const long long mod = (long long)100000000;
const long long hell = (long long)1000000007;
using namespace std;
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y % 2) res = (x % mod * res % mod) % mod;
    x = (x % mod * x % mod) % mod;
    y = y / 2;
  }
  return res;
}
long long int modinv(long long int x) { return power(x, mod - 2); }
void solve();
void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  fastIO();
  int test;
  test = 1;
  while (test--) solve();
}
long long int pw(long long int k, long long int x);
long long int gcd(long long int x, long long int y);
vector<int> gr[100001];
bool vis[100001];
bool issafe(long long int x) {
  while (x > 0) {
    if (x % 10 != 4 and x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int cnt;
void dfs(int v) {
  cnt++;
  vis[v] = true;
  for (auto u : gr[v]) {
    if (!vis[u]) dfs(u);
  }
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (!issafe(w)) {
      gr[u].push_back(v);
      gr[v].push_back(u);
    }
  }
  memset(vis, false, sizeof(vis));
  vector<long long int> temp;
  for (int i = 1; i < n + 1; i++) {
    if (vis[i] == false) {
      cnt = 0;
      dfs(i);
      temp.push_back(cnt);
    }
  }
  long long int ans = 0;
  int sz = temp.size();
  for (int i = 0; i < sz; i++) {
    long long int cur = n - temp[i];
    ans += temp[i] * (cur) * (cur - 1);
  }
  cout << ans;
}
long long int gcd(long long int a, long long int b) {
  while (b != 0) {
    long long int t = b;
    b = a % t;
    a = t;
  }
  return a;
}
long long int pw(long long int k, long long int x) {
  if (x == 0) return 1;
  if (x & 1)
    return ((pw(k, x - 1)) * k);
  else
    return ((pw(k, x / 2)) * (pw(k, x / 2)));
}
