#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-8;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 100100;
long long ans = 0;
int n, d[MAXN], dn[MAXN];
vector<int> g[MAXN];
vector<bool> good[MAXN];
bool lucky(int x) {
  while (x > 0) {
    if ((x % 10 != 4) && (x % 10 != 7)) return false;
    x /= 10;
  }
  return true;
}
int getAll(int v, int prev) {
  int &ans = d[v];
  if (ans != -1) return ans;
  ans = 1;
  for (int i = 0; i < (int)(g[v].size()); i++) {
    int u = g[v][i];
    if (u != prev) ans += getAll(u, v);
  }
  return ans;
}
int getGood(int v, int prev) {
  int &ans = dn[v];
  if (ans != -1) return ans;
  ans = 0;
  for (int i = 0; i < (int)(g[v].size()); i++) {
    int u = g[v][i];
    if (u != prev)
      if (good[v][i])
        ans += getAll(u, v);
      else
        ans += getGood(u, v);
  }
  return ans;
}
void DFS(int v, int prev, long long num) {
  long long total = num + getGood(v, prev);
  ans += total * total - total;
  for (int i = 0; i < (int)(g[v].size()); i++) {
    int u = g[v][i];
    if (u != prev)
      if (good[v][i])
        DFS(u, v, n - getAll(u, v));
      else
        DFS(u, v, total - getGood(u, v));
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < (int)(n - 1); i++) {
    int v, u, w;
    scanf("%d%d%d", &v, &u, &w);
    v--;
    u--;
    g[v].push_back(u);
    good[v].push_back(lucky(w));
    g[u].push_back(v);
    good[u].push_back(lucky(w));
  }
  memset(d, 255, sizeof d);
  memset(dn, 255, sizeof dn);
  DFS(0, 0, 0);
  cout << ans << endl;
  return 0;
}
