#include <bits/stdc++.h>
using namespace std;
const long long Maxn = 1e5 + 7;
const long long Max = 1e2 + 7;
const long long Mod = 1e9 + 7;
const long long Inf = 1e5 + 7;
const long long Const = 1000;
vector<long long> V[Maxn];
bool mark[Maxn];
long long ans, m;
bool lucky(long long h) {
  while (h > 0) {
    if (h % 10 != 4 && h % 10 != 7) return false;
    h /= 10;
  }
  return true;
}
void dfs(long long u) {
  mark[u] = true;
  m++;
  for (long long i = 0; i < V[u].size(); i++) {
    long long v = V[u][i];
    if (!mark[v]) dfs(v);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    if (!lucky(w)) V[u].push_back(v), V[v].push_back(u);
  }
  for (long long i = 1; i <= n; i++) {
    m = 0;
    if (!mark[i]) {
      dfs(i);
      ans += m * ((n - m) * (n - m - 1));
    }
  }
  cout << ans << endl;
  return 0;
}
