#include <bits/stdc++.h>
using namespace std;
const int maxn = 100001;
struct kam {};
int n, q, w, e, j[maxn], p[maxn];
long long jav;
void input();
void output();
bool isval();
void merge();
int gp(int x);
int main() {
  input();
  output();
  return 0;
}
void input() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    j[i] = 1;
  }
  for (int i = 1; i < n; i++) {
    cin >> q >> w >> e;
    if (isval()) {
      merge();
    }
  }
}
void output() {
  for (int i = 1; i <= n; i++) {
    jav += 1ll * (n - j[gp(i)]) * (n - j[gp(i)] - 1);
  }
  cout << jav;
}
bool isval() {
  while (e) {
    if (e % 10 != 7 and e % 10 != 4) {
      return 1;
    }
    e /= 10;
  }
  return 0;
}
void merge() {
  q = gp(q);
  w = gp(w);
  j[q] += j[w];
  p[w] = q;
}
int gp(int x) {
  if (x == p[x]) {
    return x;
  }
  return p[x] = gp(p[x]);
}
