#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 998244353;
long long ans = 0;
int n, val;
vector<int> g[N];
int arr[N];
bool bo[N];
bool lucky(int z) {
  while (z) {
    int u = z % 10;
    if (u != 7 && u != 4) return false;
    z /= 10;
  }
  return true;
}
void dfs(int x, vector<int>& v) {
  if (bo[x]) return;
  val++;
  bo[x] = true;
  v.emplace_back(x);
  for (auto idx : g[x]) dfs(idx, v);
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    if (!lucky(z)) {
      g[x].emplace_back(y);
      g[y].emplace_back(x);
    }
  }
  for (int i = 1; i <= n; i++) {
    val = 0;
    vector<int> v;
    if (!bo[i]) dfs(i, v);
    for (auto va : v) arr[va] = val;
  }
  for (int i = 1; i <= n; i++) {
    long long sze = 1ll * (n - arr[i]);
    ans += 1ll * (sze * (sze - 1));
  }
  cout << ans;
}
