#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O0")
#pragma GCC optimize("O1")
long long pw(long long a, long long b) {
  long long ret = 1;
  long long mul = a;
  while (b > 0) {
    if (b & 1) ret = (ret * mul) % 1000000007;
    mul = (mul * mul) % 1000000007;
    b /= 2;
  }
  return ret;
}
long long to_int(string s) {
  long long ret = 0;
  for (int(i) = (0); (i) < (s.size()); (i)++) {
    ret += pw(10, s.size() - i - 1) * (long long)(s[i] - '0');
  }
  return ret;
}
const int MAXN = 1e6;
vector<int> par(MAXN);
vector<int> rnk(MAXN);
vector<int> cmp(MAXN, 1);
int get(int v) { return par[v] = (par[v] == v ? v : get(par[v])); }
void uni(int a, int b) {
  a = get(a), b = get(b);
  if (a == b) return;
  if (rnk[a] < rnk[b]) swap(a, b);
  par[b] = a;
  rnk[a] += (rnk[a] == rnk[b]);
  cmp[a] += cmp[b];
}
bool isLucky(int w) {
  while (w) {
    if (w % 10 != 7 and w % 10 != 4) return 0;
    w /= 10;
  }
  return 1;
}
int main() {
  int n;
  cin >> n;
  for (int(i) = (0); (i) < (n); (i)++) par[i] = i;
  for (int(i) = (0); (i) < (n - 1); (i)++) {
    int s, e, w;
    cin >> s >> e >> w;
    s--, e--;
    if (!isLucky(w)) uni(s, e);
  }
  long long ans = 1ll * n * (n - 1) * (n - 2);
  for (int(i) = (0); (i) < (n); (i)++) {
    if (get(i) == i) {
      int c = cmp[get(i)];
      ans -= 1ll * c * (c - 1) * (c - 2);
      ans -= 1ll * c * (c - 1) * (n - c) * 2;
    }
  }
  cout << ans << "\n";
}
