#include <bits/stdc++.h>
using namespace std;
const int N = 1000 * 100 + 5;
vector<pair<int, int> > adj[N];
int mrk[N] = {0, 1}, d[N], a[N], aa[N], n;
int ch(int a) {
  while (a) {
    int d = a % 10;
    a /= 10;
    if (d != 4 && d != 7) return 0;
  }
  return 1;
}
void dfs(int v) {
  d[v] = 1;
  for (int i = 0; i < adj[v].size(); ++i) {
    int u = adj[v][i].second;
    int w = adj[v][i].first;
    if (!mrk[u]++) {
      dfs(u);
      d[v] += d[u];
      if (w)
        a[v] += d[u];
      else
        a[v] += a[u];
    }
  }
}
void dfsf(int v, int fa) {
  for (int i = 0; i < adj[v].size(); ++i) {
    int u = adj[v][i].second;
    int w = adj[v][i].first;
    if (u != fa) {
      if (w)
        aa[u] = n - d[u] + a[u];
      else
        aa[u] = aa[v];
      dfsf(u, v);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int e, b, cost;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    cin >> e >> b >> cost;
    int tmp = ch(cost);
    adj[e].push_back(make_pair(tmp, b));
    adj[b].push_back(make_pair(tmp, e));
  }
  dfs(1);
  aa[1] = a[1];
  dfsf(1, -1);
  long long sum = 0;
  for (int i = 1; i <= n; ++i) sum += (long long)aa[i] * (aa[i] - 1);
  cout << sum << endl;
  return 0;
}
