#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e6 + 10;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
struct edge {
  int u, v, num;
  edge(int a, int b, int c) { u = a, v = b, num = c; }
};
vector<edge> g[MAXN];
long long subtree[MAXN], f[MAXN], fprim[MAXN];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    g[u].emplace_back(edge(u, v, w));
    g[v].emplace_back(edge(v, u, w));
  }
  auto lucky = [&](int n) {
    while (n) {
      int yekan = n % 10;
      if (yekan != 7 && yekan != 4) return false;
      n /= 10;
    }
    return true;
  };
  function<void(int, int)> subtree_calc = [&](int u, int p) {
    subtree[u] = 1;
    for (auto child : g[u]) {
      if (child.v == p) continue;
      subtree_calc(child.v, child.u);
      subtree[u] += subtree[child.v];
    }
    return;
  };
  function<void(int, int)> fcalc = [&](int u, int p) {
    f[u] = 0;
    for (auto child : g[u]) {
      if (child.v == p) continue;
      if (lucky(child.num)) {
        f[u] += subtree[child.v];
        fcalc(child.v, child.u);
      } else {
        fcalc(child.v, child.u);
        f[u] += f[child.v];
      }
    }
    return;
  };
  function<void(int, edge)> fprimcalc = [&](int u, edge p) {
    if (u == 1) {
      fprim[u] = 0;
      for (auto child : g[u]) fprimcalc(child.v, child);
      return;
    }
    if (lucky(p.num)) {
      fprim[u] = n - subtree[u];
    } else {
      fprim[u] = fprim[p.u] + f[p.u] - f[p.v];
    }
    for (auto child : g[u]) {
      if (child.v == p.u) continue;
      fprimcalc(child.v, child);
    }
    return;
  };
  subtree_calc(1, 0);
  fcalc(1, 0);
  fprimcalc(1, edge(0, 0, 0));
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += f[i] * (f[i] - 1) + fprim[i] * (fprim[i] - 1) + 2 * f[i] * fprim[i];
  }
  printf("%lld\n", ans);
  return 0;
}
