#include <bits/stdc++.h>
using namespace std;
bool isLucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
vector<pair<int, int> > v[100005];
int dp[100005], sub[100005], dp2[100005];
void dfs(int x, int p) {
  for (auto i : v[x]) {
    if (i.first == p) continue;
    dfs(i.first, x);
    sub[x] += sub[i.first];
    if (i.second == 1) {
      dp[x] += sub[i.first];
    } else
      dp[x] += dp[i.first];
  }
  sub[x]++;
}
int n;
long long ans = 0;
long long NP2(long long x) { return x * (x - 1); }
void dfs2(int x, int p, int w) {
  if (w == 1) {
    dp2[x] += n - sub[x];
  } else {
    dp2[x] += dp2[p] + dp[p] - dp[x];
  }
  for (auto i : v[x]) {
    if (i.first == p) continue;
    dfs2(i.first, x, i.second);
  }
  ans += NP2(dp[x] + dp2[x]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    w = isLucky(w);
    v[x].push_back({y, w});
    v[y].push_back({x, w});
  }
  dfs(1, 0);
  dp[0] = dp[1];
  dfs2(1, 0, 0);
  cout << ans;
}
