#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[1 << 17];
long long n, sz[1 << 17], fromparent[1 << 17], fromchilds[1 << 17];
bool islucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
long long dfs(int x, int par) {
  sz[x] = 1;
  fromparent[x] = fromchilds[x] = 0;
  int s = adj[x].size();
  pair<int, int> p;
  int &nxt = p.first, &w = p.second;
  for (int i = 0; i < s; ++i) {
    p = adj[x][i];
    if (nxt == par) continue;
    sz[x] += dfs(nxt, x);
    if (w)
      fromchilds[x] += sz[nxt];
    else
      fromchilds[x] += fromchilds[nxt];
  }
  return sz[x];
}
void dfs1(int x, int par) {
  int s = adj[x].size();
  pair<int, int> p;
  int &nxt = p.first, &w = p.second;
  for (int i = 0; i < s; ++i) {
    p = adj[x][i];
    if (nxt == par) continue;
    if (w)
      fromparent[nxt] = n - sz[nxt];
    else
      fromparent[nxt] = fromparent[x] + fromchilds[x] - fromchilds[nxt];
    dfs1(nxt, x);
  }
}
int main() {
  cin >> n;
  int u, v, w;
  for (int i = 1; i < n; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    w = islucky(w);
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  dfs(1, 0);
  dfs1(1, 0);
  long long ans = 0, z;
  for (int i = 1; i <= n; ++i) {
    z = fromparent[i] + fromchilds[i];
    ans += z * (z - 1);
  }
  cout << ans;
}
