#include <bits/stdc++.h>
using namespace std;
const int N = 1e+5 + 5;
long long int sz[N], mans[N], ans, n;
vector<pair<int, int>> adj[N];
int islucky(int w) {
  while (w > 0) {
    if (!(w % 10 == 7 || w % 10 == 4)) return 0;
    w /= 10;
  }
  return 1;
}
void dfs(int v, int p) {
  sz[v] = 1;
  for (auto it : adj[v]) {
    if (it.first != p) {
      dfs(it.first, v);
      sz[v] += sz[it.first];
      if (it.second == 1)
        mans[v] += sz[it.first];
      else
        mans[v] += mans[it.first];
    }
  }
}
void filans(int v, int p) {
  ans += (mans[v] * (mans[v] - 1));
  for (auto it : adj[v]) {
    if (it.first != p) {
      if (it.second == 1)
        mans[it.first] = mans[it.first] + n - sz[it.first];
      else {
        mans[it.first] = mans[v];
      }
      filans(it.first, v);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[u].push_back(make_pair(v, islucky(w)));
    adj[v].push_back(make_pair(u, islucky(w)));
  }
  dfs(1, 0);
  filans(1, 0);
  cout << ans;
}
