#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int> > adj[100010];
int totalbaxo[100010], totalcima[100010];
int respbaxo[100010], respcima[100010];
inline bool ehlucky(int n) {
  if (n == 0) return false;
  while (n > 0) {
    if ((n % 10) != 4 and (n % 10) != 7) return false;
    n /= 10;
  }
  return true;
}
void dfs0(int u, int paiu) {
  int soma = 1;
  for (int i = 0; i < (int)adj[u].size(); i++)
    if (adj[u][i].first != paiu) {
      dfs0(adj[u][i].first, u);
      soma += totalbaxo[adj[u][i].first];
    }
  totalbaxo[u] = soma;
}
void dfs1(int u, int paiu) {
  if (paiu != -1)
    totalcima[u] = totalcima[paiu] + totalbaxo[paiu] - totalbaxo[u];
  else
    totalcima[u] = 0;
  for (int i = 0; i < (int)adj[u].size(); i++)
    if (adj[u][i].first != paiu) dfs1(adj[u][i].first, u);
}
void dfs2(int u, int paiu) {
  int soma = 0;
  for (int i = 0; i < (int)adj[u].size(); i++)
    if (adj[u][i].first != paiu) {
      dfs2(adj[u][i].first, u);
      if (ehlucky(adj[u][i].second))
        soma += totalbaxo[adj[u][i].first];
      else
        soma += respbaxo[adj[u][i].first];
    }
  respbaxo[u] = soma;
}
void dfs3(int u, int paiu, int w) {
  if (ehlucky(w))
    respcima[u] = totalcima[u];
  else {
    if (paiu == -1)
      respcima[u] = 0;
    else
      respcima[u] = respcima[paiu] + respbaxo[paiu] - respbaxo[u];
  }
  for (int i = 0; i < (int)adj[u].size(); i++)
    if (adj[u][i].first != paiu) dfs3(adj[u][i].first, u, adj[u][i].second);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) adj[i].clear();
  for (int i = 0; i < n - 1; i++) {
    int v1, v2, w;
    scanf("%d %d %d", &v1, &v2, &w);
    v1--;
    v2--;
    adj[v1].push_back(pair<int, int>(v2, w));
    adj[v2].push_back(pair<int, int>(v1, w));
  }
  dfs0(0, -1);
  dfs1(0, -1);
  dfs2(0, -1);
  dfs3(0, -1, 1);
  long long int tot = 0;
  for (int i = 0; i < n; i++) {
    long long int t = respcima[i] + respbaxo[i];
    tot += t * (t - 1);
  }
  cout << tot << endl;
  return 0;
}
