#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > adj_lis;
int dp1[100009], dp2[100009];
int dfs(int u, int p) {
  if (dp1[u] != -1) return dp1[u];
  int ans = 0;
  for (vector<pair<int, int> >::iterator it = (adj_lis[u]).begin();
       it != (adj_lis[u]).end(); it++) {
    if (it->second == 0 && it->first != p) ans += 1 + dfs(it->first, u);
    if (it->first != p) dfs(it->first, u);
  }
  return dp1[u] = ans;
}
void dfs2(int u, int p) {
  for (vector<pair<int, int> >::iterator it = (adj_lis[u]).begin();
       it != (adj_lis[u]).end(); it++) {
    if (it->first != p) {
      if (it->second == 0) dp2[it->first] = dp2[u] + dp1[u] - dp1[it->first];
      dfs2(it->first, u);
    }
  }
}
int main() {
  memset(dp1, -1, sizeof(dp1));
  int n;
  scanf("%d", &n);
  adj_lis.resize(n + 1);
  for (int i = int(0); i <= int(n - 2); i++) {
    int p, q, w;
    scanf("%d", &p);
    scanf("%d", &q);
    scanf("%d", &w);
    bool ch = true;
    while (w > 0) {
      if (w % 10 != 4 && w % 10 != 7) {
        ch = false;
        break;
      }
      w /= 10;
    }
    if (ch)
      w = 1;
    else
      w = 0;
    adj_lis[p].push_back(pair<int, int>(q, w));
    adj_lis[q].push_back(pair<int, int>(p, w));
  }
  dfs(1, 0);
  dfs2(1, 0);
  long long int ans = 0;
  for (int i = int(1); i <= int(n); i++) {
    int val = n - 1 - dp1[i] - dp2[i];
    ans += (val)*1ll * (val - 1);
  }
  printf("%lld\n", ans);
  return 0;
}
