#include <bits/stdc++.h>
using namespace std;
const int inf = 100009;
vector<pair<int, bool> > adj[inf];
long long sol = 0;
int n;
int sz[inf];
int dp[inf];
bool islucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int dfs(int x, int p) {
  sz[x] = 1;
  for (int i = 0; i < (int)adj[x].size(); ++i) {
    if (adj[x][i].first != p) {
      int h = dfs(adj[x][i].first, x);
      dp[x] += adj[x][i].second ? h : dp[adj[x][i].first];
      sz[x] += h;
    }
  }
  return sz[x];
}
void solve(int x, int p, long long s) {
  sol += s * (s - 1);
  for (int i = 0; i < (int)adj[x].size(); ++i) {
    if (adj[x][i].first == p) continue;
    if (adj[x][i].second)
      solve(adj[x][i].first, x,
            dp[adj[x][i].first] + (n - sz[adj[x][i].first]));
    else
      solve(adj[x][i].first, x, s);
  }
}
int main() {
  int a, b, c;
  bool g;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    scanf("%d%d%d", &a, &b, &c);
    g = islucky(c);
    adj[a].push_back(make_pair(b, g));
    adj[b].push_back(make_pair(a, g));
  }
  dfs(1, 1);
  solve(1, 1, dp[1]);
  printf("%I64d\n", sol);
}
