#include <bits/stdc++.h>
using namespace std;
long long cnt[100010];
long long sz[100010];
long long visited[100010];
vector<vector<pair<long long, long long>>> vt;
void dfs(int here) {
  visited[here] = 1;
  sz[here] = 1;
  for (int i = 0; i < vt[here].size(); i++) {
    long long next = vt[here][i].first;
    long long ncst = vt[here][i].second;
    if (!visited[next]) {
      dfs(next);
      sz[here] += sz[next];
      int ch = 0;
      while (ncst) {
        if (ncst % 10 != 4 && ncst % 10 != 7) ch = 1;
        ncst /= 10;
      }
      if (!ch)
        cnt[here] += sz[next];
      else
        cnt[here] += cnt[next];
    }
  }
}
long long n;
long long dap = 0;
void dfs1(long long here, long long ch) {
  visited[here] = 1;
  dap += ((ch + cnt[here]) * (ch + cnt[here] - 1));
  for (int i = 0; i < vt[here].size(); i++) {
    long long next = vt[here][i].first;
    long long ncst = vt[here][i].second;
    if (!visited[next]) {
      int cc = 0;
      while (ncst) {
        if (ncst % 10 != 4 && ncst % 10 != 7) cc = 1;
        ncst /= 10;
      }
      if (!cc)
        dfs1(next, n - sz[next]);
      else
        dfs1(next, ch + cnt[here] - cnt[next]);
    }
  }
}
int main() {
  scanf("%lld", &n);
  vt.resize(n + 1);
  for (int i = 0; i < n - 1; i++) {
    long long q, w, e;
    scanf("%lld %lld %lld", &q, &w, &e);
    vt[q].push_back({w, e});
    vt[w].push_back({q, e});
  }
  dfs(1);
  memset(visited, 0, sizeof(visited));
  dfs1(1, 0);
  printf("%lld", dap);
}
