#include <bits/stdc++.h>
using namespace std;
const int oo = 2000000000;
const int N = 100005;
int qu[N], num[N], n, m;
bool F[N];
vector<int> g[N], edge[N];
void bfs(int u) {
  F[u] = true;
  int fr = 1, re = 1;
  qu[1] = u;
  while (fr <= re) {
    int u = qu[fr++];
    for (int i = 0; i < (int)g[u].size(); ++i) {
      int v = g[u][i];
      if (!edge[u][i] && !F[v]) {
        F[v] = true;
        qu[++re] = v;
      }
    }
  }
  for (int i = 1; i <= re; ++i) num[qu[i]] = re;
}
long long C2(long long n) { return (n - 1) * n; }
bool check(int x) {
  for (; x; x /= 10)
    if (x % 10 != 4 && x % 10 != 7) return false;
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    if (check(c))
      c = 1;
    else
      c = 0;
    g[u].push_back(v);
    edge[u].push_back(c);
    g[v].push_back(u);
    edge[v].push_back(c);
  }
  for (int i = 1; i <= n; ++i)
    if (!num[i]) bfs(i);
  long long res = 0;
  for (int i = 1; i <= n; ++i) res += C2(n - num[i]);
  cout << res << endl;
  return 0;
}
