#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, bool>>> verts;
vector<int> svyasn(1, 0);
vector<int> inds;
vector<bool> check;
void func(int vert, int ind) {
  svyasn[ind]++;
  inds[vert] = ind;
  check[vert] = true;
  for (int(i) = 0; (i) < (verts[vert].size()); (i)++)
    if (!check[verts[vert][i].first]) {
      if (verts[vert][i].second) {
        svyasn.push_back(0);
        func(verts[vert][i].first, svyasn.size() - 1);
      } else
        func(verts[vert][i].first, ind);
    }
}
int main() {
  int n;
  cin >> n;
  verts.resize(n);
  for (int(i) = 0; (i) < (n - 1); (i)++) {
    int x, y, w;
    cin >> x >> y >> w;
    bool luck = true;
    while (w > 0) {
      luck = (w % 10 == 4) || (w % 10 == 7);
      if (!luck) break;
      w /= 10;
    }
    x--;
    y--;
    verts[x].push_back(pair<int, bool>(y, luck));
    verts[y].push_back(pair<int, bool>(x, luck));
  }
  inds.resize(n);
  check.resize(n, false);
  func(0, 0);
  long long res = 0;
  for (int(i) = 0; (i) < (n); (i)++)
    res += (long long)(n - svyasn[inds[i]]) * (n - svyasn[inds[i]] - 1);
  cout << res;
}
