#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g[100010];
long long cnt[100010];
long long cntup[100010];
int sum[100010];
int n;
bool isspecial(int n) {
  while (n) {
    int t = n % 10;
    if (t != 7 && t != 4) return false;
    n /= 10;
  }
  return true;
}
void dfs(int cur, int par) {
  sum[cur] = 1;
  for (auto next : g[cur]) {
    if (next.first != par) {
      dfs(next.first, cur);
      if (next.second) {
        cnt[cur] += sum[next.first];
      } else {
        cnt[cur] += cnt[next.first];
      }
      sum[cur] += sum[next.first];
    }
  }
}
void go(int cur, int par) {
  for (auto next : g[cur]) {
    if (next.first != par) {
      if (next.second) {
        cntup[next.first] += n - sum[next.first];
      } else {
        cntup[next.first] += cntup[cur] + cnt[cur] - cnt[next.first];
      }
      go(next.first, cur);
    }
  }
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    if (isspecial(w))
      w = 1;
    else
      w = 0;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  dfs(1, 1);
  go(1, 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long tot = cnt[i] + cntup[i];
    ans += (tot * (tot - 1));
  }
  cout << ans << endl;
  return 0;
}
