#include <bits/stdc++.h>
using namespace std;
int cnt;
vector<list<int> > a;
list<int> x;
char v[100010];
void dfs(int in, int p) {
  v[in] = 1;
  cnt++;
  int i, j, k, l;
  list<int>::iterator it;
  for (it = a[in].begin(); it != a[in].end(); it++) {
    if (*it != p) {
      dfs(*it, in);
    }
  }
}
int main() {
  int i, j, k, l, m, n, p, q;
  char s[25];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    a.push_back(x);
  }
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d%s", &p, &k, s);
    m = strlen(s);
    l = 0;
    for (j = 0; j < m; j++) {
      if (!(s[j] == '4' || s[j] == '7')) {
        l = 1;
        break;
      }
    }
    if (l != 0) {
      a[p - 1].push_back(k - 1);
      a[k - 1].push_back(p - 1);
    }
  }
  vector<int> ans;
  for (i = 0; i < n; i++) {
    cnt = 0;
    if (v[i] == 0) {
      dfs(i, -1);
      ans.push_back(cnt);
    }
  }
  l = ans.size();
  long long xx, yy;
  long long fin = (long long)n * (long long)(n - 1) * (long long)(n - 2);
  for (i = 0; i < l; i++) {
    if (ans[i] >= 2) {
      xx = (long long)ans[i] * (long long)(ans[i] - 1) * 2 * (n - ans[i]);
      fin -= xx;
    }
    if (ans[i] >= 3) {
      xx =
          (long long)ans[i] * (long long)(ans[i] - 1) * (long long)(ans[i] - 2);
      fin -= xx;
    }
  }
  cout << fin << endl;
  return 0;
}
