#include <bits/stdc++.h>
using namespace std;
const int MAX = 1 << 20;
vector<pair<int, int> > adj[1 << 20];
int n;
long long G[MAX], CNT[MAX], B[MAX];
bool isLucky(int val) {
  while (val) {
    int d = val % 10;
    val /= 10;
    if (d != 7 && d != 4) return 0;
  }
  return 1;
}
void dfs(int u, int p) {
  CNT[u] = 1;
  for (auto e : adj[u])
    if (e.first != p) {
      dfs(e.first, u);
      CNT[u] += CNT[e.first];
      if (isLucky(e.second))
        G[u] += CNT[e.first];
      else
        G[u] += G[e.first];
    }
}
void dfs(int u, int p, long long v) {
  B[u] = v;
  for (auto e : adj[u])
    if (e.first != p) {
      if (isLucky(e.second))
        dfs(e.first, u, CNT[1] - CNT[e.first]);
      else
        dfs(e.first, u, B[u] + G[u] - G[e.first]);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n - 1); i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    adj[a].push_back(make_pair(b, c));
    adj[b].push_back(make_pair(a, c));
  }
  dfs(1, 0);
  dfs(1, 0, 0);
  long long ans = 0;
  for (int i = (1); i <= (n); i++)
    ans += G[i] * (G[i] - 1) + B[i] * (B[i] - 1) + 2 * G[i] * B[i];
  cout << ans << endl;
  cerr << ans << endl;
  return 0;
}
