#include <bits/stdc++.h>
using namespace std;
const int mx1 = 30;
const int mx2 = 505;
const int mx3 = 1005;
const int mx4 = 30005;
const int mx5 = 100005;
const int mx6 = 1000005;
int _toggle(int N, int pos) { return N = N ^ (1 << pos); }
int _set(int N, int pos) { return N = N | (1 << pos); }
int _reset(int N, int pos) { return N = N & ~(1 << pos); }
bool _check(int N, int pos) { return (bool)(N & (1 << pos)); }
bool _upper(char a) { return a >= 'A' && a <= 'Z'; }
bool _lower(char a) { return a >= 'a' && a <= 'z'; }
bool _digit(char a) { return a >= '0' && a <= '9'; }
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
int pr[mx5], sz[mx5];
struct dsu {
  dsu(int n) {
    for (int i = 1; i <= n; i++) {
      pr[i] = i, sz[i] = 1;
    }
  }
  int _find(int x) { return pr[x] == x ? x : pr[x] = _find(pr[x]); }
  void _connect(int x, int y) {
    x = _find(x), y = _find(y);
    if (sz[x] > sz[y]) {
      pr[y] = x, sz[x] += sz[y];
    } else {
      pr[x] = y, sz[y] += sz[x];
    }
  }
  int _sz(int x) { return sz[_find(x)]; }
};
bool lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) {
      return 0;
    }
    x /= 10;
  }
  return 1;
}
void solve() {
  int n;
  cin >> n;
  dsu uf(n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (!lucky(w)) {
      uf._connect(u, v);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long rest = n - uf._sz(i);
    ans += rest * (rest - 1);
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc = 1;
  while (tc--) {
    solve();
  }
}
