#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g[100010];
int ile[100010];
int ile1[100010];
int subtree[100010];
long long res;
int n, root;
bool isLucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
void dfs(int nr, int par) {
  subtree[nr] = 1;
  for (int i = 0; i < ((int)g[nr].size()); ++i)
    if (g[nr][i].first != par) {
      dfs(g[nr][i].first, nr);
      if (isLucky(g[nr][i].second))
        res +=
            subtree[g[nr][i].first] * (long long)(subtree[g[nr][i].first] - 1);
      else
        res += ile[g[nr][i].first] * (long long)(ile[g[nr][i].first] - 1);
    }
  int sum = 0;
  for (int i = 0; i < ((int)g[nr].size()); ++i)
    if (g[nr][i].first != par)
      if (isLucky(g[nr][i].second))
        sum += subtree[g[nr][i].first];
      else
        sum += ile[g[nr][i].first];
  for (int i = 0; i < ((int)g[nr].size()); ++i)
    if (g[nr][i].first != par)
      if (isLucky(g[nr][i].second))
        res += subtree[g[nr][i].first] *
               (long long)(sum - subtree[g[nr][i].first]);
      else
        res += ile[g[nr][i].first] * (long long)(sum - ile[g[nr][i].first]);
  ile[nr] = sum;
  for (int i = 0; i < ((int)g[nr].size()); ++i)
    if (g[nr][i].first != par) subtree[nr] += subtree[g[nr][i].first];
}
void dfs1(int nr, int par) {
  for (int i = 0; i < ((int)g[nr].size()); ++i)
    if (g[nr][i].first == par) {
      int pom;
      if (isLucky(g[nr][i].second))
        pom = n - subtree[nr];
      else
        pom = ile1[g[nr][i].first] + ile[g[nr][i].first] - ile[nr];
      res += pom * (long long)(pom - 1);
      res += 2 * pom * (long long)ile[nr];
      ile1[nr] = pom;
    }
  for (int i = 0; i < ((int)g[nr].size()); ++i)
    if (g[nr][i].first != par) dfs1(g[nr][i].first, nr);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (n - 1); ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--;
    b--;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }
  dfs(0, -1);
  dfs1(0, -1);
  printf("%lld\n", res);
  return 0;
}
