#include <bits/stdc++.h>
using namespace std;
long long n, x, y, k, h, i, ts, res, a[100001], b[100001], c[100001], d[100001],
    root[100001], high[100001], sl[100001];
bool p;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    high[i] = root[i] = 0;
    sl[i] = 1;
  }
  for (i = 1; i < n; i++) {
    cin >> x >> y >> k;
    p = true;
    do {
      h = k % 10;
      if (h != 7 && h != 4) p = false;
      k /= 10;
    } while (k != 0 && p == true);
    if (p == false) {
      h = x;
      if (root[h] != 0) do {
          h = root[h];
        } while (root[h] != 0);
      k = y;
      if (root[k] != 0) do {
          k = root[k];
        } while (root[k] != 0);
      if (high[h] == high[k]) {
        root[h] = k;
        high[k]++;
        sl[k] += sl[h];
      } else if (high[h] < high[k]) {
        root[h] = k;
        sl[k] += sl[h];
      } else {
        root[k] = h;
        sl[h] += sl[k];
      }
    }
  }
  ts = 0;
  b[0] = 0;
  d[0] = 0;
  for (i = 1; i <= n; i++)
    if (root[i] == 0) {
      ts++;
      a[ts] = sl[i];
      b[ts] = b[ts - 1] + a[ts];
      c[ts] = a[ts] * (b[ts - 1]);
      d[ts] = d[ts - 1] + c[ts];
      res += a[ts] * d[ts - 1];
    }
  res *= 6;
  for (i = 1; i <= ts; i++)
    if (a[i] >= 2) res += (a[i] * (a[i] - 1)) * (n - a[i]);
  cout << res;
}
