#include <bits/stdc++.h>
using namespace std;
using i64 = long long int;
using f80 = long double;
i64 f(int n) { return i64(n) * (n - 1); }
const int N = 100005;
bitset<N> vis;
vector<pair<int, bool>> g[N];
int color[N];
bool lucky(int x) {
  while (x) {
    if (((x % 10) != 4) && ((x % 10) != 7)) return 0;
    x /= 10;
  }
  return 1;
}
void dfs(int u, int c) {
  color[c]++;
  vis[u] = 1;
  for (auto v : g[u]) {
    if (v.second || vis[v.first]) continue;
    dfs(v.first, c);
  }
}
signed main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    w = lucky(w);
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  int c = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) dfs(i, c++);
  }
  i64 ans = 0;
  for (int i = 1; i < c; i++) {
    ans += f(n - color[i]) * color[i];
  }
  printf("%lld", ans);
  return 0;
}
