#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int const N = 100 * 1000 + 16;
struct dsu {
  vector<int> fa;
  dsu(int n) { fa.assign(n + 1, -1); }
  int find(int x) { return fa[x] < 0 ? x : fa[x] = find(fa[x]); }
  int join(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return 0;
    fa[x] += fa[y];
    fa[y] = x;
    return 1;
  }
  int sz(int x) { return -fa[find(x)]; }
};
int n;
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  dsu d(n);
  vector<pair<int, int>> a;
  for (int i = 1; i < n; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    string s = to_string(w);
    w = all_of(s.begin(), s.end(),
               [](char c) { return (c == '4' || '7' == c); });
    if (w)
      a.emplace_back(u, v);
    else
      d.join(u, v);
  }
  ll ans = 0;
  for (int i = 1; i <= n; ++i) ans += 1ll * (n - d.sz(i)) * (n - d.sz(i) - 1);
  cout << ans;
}
