#include <bits/stdc++.h>
using namespace std;
int i, n, k, N, M, K;
vector<pair<int, int> > v[100010];
int achild[100010], child[100010], apar[100010], par[100010];
long long sum;
int lucky(int x) {
  while (x != 0) {
    if (x % 10 != 7 and x % 10 != 4) return 0;
    x /= 10;
  }
  return 1;
}
void dfs(int x, int prev) {
  int a;
  child[x] = 0;
  achild[x] = 0;
  for (a = 0; a < v[x].size(); a++) {
    if (v[x][a].first == prev) continue;
    dfs(v[x][a].first, x);
    if (v[x][a].second == 1) {
      achild[x] += child[v[x][a].first] + 1;
    } else {
      achild[x] += achild[v[x][a].first];
    }
    child[x] += child[v[x][a].first] + 1;
  }
  par[x] = N - child[x] - 1;
}
void find_par(int x, int prev) {
  int a;
  for (a = 0; a < v[x].size(); a++) {
    if (v[x][a].first == prev) continue;
    int u = v[x][a].first;
    if (v[x][a].second) {
      apar[u] = par[u];
    } else {
      apar[u] += apar[x];
      apar[u] += achild[x] - achild[u];
    }
  }
  for (a = 0; a < v[x].size(); a++) {
    if (v[x][a].first == prev) continue;
    find_par(v[x][a].first, x);
  }
}
int main() {
  int a, b, c, d;
  cin >> N;
  for (a = 0; a < N - 1; a++) {
    scanf("%d %d %d", &c, &d, &b);
    v[c].push_back(make_pair(d, lucky(b)));
    v[d].push_back(make_pair(c, lucky(b)));
  }
  dfs(1, -1);
  find_par(1, -1);
  for (a = 1; a <= N; a++) {
    sum += (long long)((long long)achild[a] * (achild[a] - 1));
    sum += (long long)apar[a] * (long long)achild[a] * 2;
    sum += (long long)((long long)apar[a] * (apar[a] - 1));
  }
  cout << sum;
  return 0;
}
