#include <bits/stdc++.h>
using namespace std;
bool lucky(int in) {
  while (in) {
    if (in % 10 != 4 && in % 10 != 7) return 0;
    in = in / 10;
  }
  return 1;
}
vector<int> graf[100009];
vector<bool> luck[100009];
int n;
bool visit[100009];
long long c[100009], s[100009];
void dfs(int u, int p) {
  s[u] = 1;
  for (int i = 0; i < graf[u].size(); i++) {
    int v = graf[u][i];
    if (v == p) continue;
    dfs(v, u);
    s[u] += s[v];
    if (luck[u][i])
      c[u] += s[v];
    else
      c[u] += c[v];
  }
}
long long ans = 0;
void dfs2(int u, int p, long long d) {
  ans += (d + c[u]) * (d + c[u] - 1);
  for (int i = 0; i < graf[u].size(); i++) {
    int v = graf[u][i];
    if (v == p) continue;
    long long td;
    if (luck[u][i])
      td = n - s[v];
    else
      td = d + c[u] - c[v];
    dfs2(v, u, td);
  }
}
int main() {
  cin >> n;
  int a, b, w;
  for (int i = 1; i < n; i++) {
    cin >> a >> b >> w;
    graf[a].push_back(b);
    luck[a].push_back(lucky(w));
    graf[b].push_back(a);
    luck[b].push_back(lucky(w));
  }
  memset(s, 0, sizeof(s));
  memset(c, 0, sizeof(c));
  dfs(1, 0);
  dfs2(1, 0, 0);
  cout << ans << endl;
  return 0;
}
