#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
long long n, ans, U[N], D[N], sz[N];
vector<pair<long long, long long> > nei[N];
map<long long, bool> luc;
bool check(long long w) {
  while (w) {
    if (w % 10 != 4 && w % 10 != 7) {
      return false;
    }
    w /= 10;
  }
  return true;
}
void read_input() {
  cin >> n;
  for (long long i = 1; i < n; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    nei[u].push_back({v, w});
    nei[v].push_back({u, w});
    luc[w] = check(w);
  }
}
void how(long long v, long long par) {
  sz[v]++;
  for (auto u : nei[v]) {
    if (u.first != par) {
      how(u.first, v);
      sz[v] += sz[u.first];
    }
  }
}
void down(long long v, long long par) {
  for (auto u : nei[v]) {
    if (u.first != par) {
      down(u.first, v);
      if (luc[u.second]) {
        D[v] += sz[u.first];
      } else {
        D[v] += D[u.first];
      }
    }
  }
}
void up(long long v, long long par) {
  for (auto u : nei[v]) {
    if (u.first != par) {
      if (luc[u.second]) {
        U[u.first] = n - sz[u.first];
      } else {
        U[u.first] = D[v] - D[u.first] + U[v];
      }
      up(u.first, v);
    }
  }
}
void write_output() {
  for (long long i = 1; i <= n; i++) {
    ans += D[i] * (D[i] - 1) + U[i] * (U[i] - 1) + 2 * U[i] * D[i];
  }
  cout << ans;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  read_input();
  how(1, -1);
  down(1, -1);
  up(1, -1);
  write_output();
  return 0;
}
