#include <bits/stdc++.h>
#pragma comment(linker, "/stack:247474112")
#pragma GCC optimize("Ofast")
using namespace std;
const long long mod = 1e9 + 7;
const int inf = 1e9;
const long long inflong = 1e18;
int setbit(int n, int pos) { return n = n | (1 << pos); }
int resetbit(int n, int pos) { return n = n & ~(1 << pos); }
bool checkbit(int n, int pos) { return (bool)(n & (1 << pos)); }
template <typename T>
void print(const T& v) {
  cerr << v << ' ';
}
template <typename T>
T modMul(T x, T y) {
  x %= mod;
  y %= mod;
  return (x * y) % mod;
}
template <typename T>
T modAdd(T x, T y) {
  x %= mod;
  y %= mod;
  return (x + y) % mod;
}
template <typename T>
T modPow(T x, T k, T mod) {
  if (k == 0) return 1;
  if (k & 1) {
    return ((x % mod) * modPow(x, k - 1, mod) % mod) % mod;
  } else {
    T ret = modPow(x, k / 2, mod);
    ret %= mod;
    return (ret * ret) % mod;
  }
}
template <typename T1, typename... T2>
void print(const T1& first, const T2&... rest) {
  print(first);
  print(rest...);
}
template <typename T>
T ext_gcd(T num1, T num2, T& X, T& Y) {
  T r2, r1, q2, q1, x2, x1, y2, y1, x, y, r;
  x2 = 1;
  y2 = 0;
  x1 = 0;
  y1 = 1;
  for (r2 = num1, r1 = num2; r1 != 0;
       y2 = y1, y1 = y, x2 = x1, x1 = x, r2 = r1, r1 = r) {
    q1 = r2 / r1;
    x = x2 - q1 * x1;
    y = y2 - q1 * y1;
    r = r2 % r1;
  }
  X = x2;
  Y = y2;
  return r2;
}
template <typename T>
T modInv(T x, T m) {
  return modPow(x, m - 2, m);
  long long ret, t;
  ext_gcd(x, m, ret, t);
  ret %= m;
  if (ret < 0) ret += m;
  return ret;
}
const int M = 2000123;
const int mx = M / 2 + 2;
vector<int> g[M + 2];
int par[M + 2], cnt[M + 2];
int get(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = get(par[x]);
}
void sett(int xx, int yy) {
  int x = get(xx), y = get(yy);
  if (x == y) return;
  par[x] = y;
  cnt[y] += cnt[x];
}
bool lucky(int x) {
  int f = 0;
  while (x) {
    int t = x % 10;
    if (t != 4 and t != 7) return 0;
    x /= 10;
  }
  return 1;
}
void solve() {
  int n, x, y, w;
  scanf("%d", &n);
  ;
  for (int i = 1; i <= n; i++) par[i] = i;
  for (int i = 1; i <= n; i++) cnt[i] = 1;
  for (int i = 0; i < n - 1; i++) {
    scanf("%d%d", &x, &y);
    ;
    scanf("%d", &w);
    ;
    if (!lucky(w)) {
      sett(x, y);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long x = (long long)(n - cnt[get(i)]);
    ans += (x * (x - 1));
  }
  cout << ans << "\n";
}
int main() {
  int x, cnt = 0, t = 1;
  x = 14;
  while (t--) solve();
  return 0;
}
