#include <bits/stdc++.h>
const int MXN = 1e6 + 5;
const long long MNN = 1e5 + 1;
const long long MOD = 1000000007;
const long long INF = 1e18;
const int OO = 1e9 + 500;
using namespace std;
long long n, cnt[MXN], num[MXN], up[MXN];
long long ans;
vector<pair<long long, long long> > g[MXN];
map<int, bool> mp;
void dfs(int x, int p) {
  cnt[x] = 1;
  for (auto i : g[x]) {
    if (i.first == p) continue;
    dfs(i.first, x);
    if (mp[i.second] == 1) {
      num[x] += cnt[i.first];
    } else {
      num[x] += num[i.first];
    }
    cnt[x] += cnt[i.first];
  }
}
void dfs1(int x, int p) {
  for (auto i : g[x]) {
    if (i.first == p) continue;
    if (mp[i.second] == 1) {
      up[i.first] = n - cnt[i.first];
    } else {
      up[i.first] = up[x] + num[x] - num[i.first];
    }
    dfs1(i.first, x);
  }
}
void goodNum(long long t) {
  if (t > OO) {
    return;
  }
  mp[t] = 1;
  goodNum(t * 10 + 4);
  goodNum(t * 10 + 7);
}
int main() {
  ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
  ;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  goodNum(0);
  dfs(1, 0);
  dfs1(1, 0);
  for (int i = 1; i <= n; i++) {
    ans += num[i] * (num[i] - 1);
    ans += up[i] * (up[i] - 1);
    ans += 2 * up[i] * num[i];
  }
  cout << ans;
  return 0;
}
