#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const long long INF = 1e18 + 5;
const long long mxn = 1e6 + 5;
const long long mod = 1e9 + 7;
long long n, a[mxn], u[mxn], p[mxn], iii;
long long b[mxn];
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res *= a;
    res %= mod;
    a *= a;
    a %= mod;
    n >>= 1;
  }
  return res;
}
long long c[mxn];
long long ge(long long x, long long y) {
  if (x == y && y == 1) return 0;
  if (x == y) return 1e9;
  if (x < y) swap(x, y);
  return ge(x - y, y) + 1;
}
long long dp[mxn], cnt[mxn];
vector<long long> g[mxn];
vector<pair<int, int> > y[mxn];
map<pair<int, int>, int> m;
int d[mxn];
void dfs(int v) {
  u[v] = 1;
  cnt[v] = 1;
  for (auto to : g[v]) {
    int y = m[make_pair(v, to)];
    if (u[to] == 1) continue;
    dfs(to);
    cnt[v] += cnt[to];
    if (y)
      d[v] += cnt[to];
    else
      d[v] += d[to];
  }
}
void df(int v) {
  u[v] = 1;
  for (auto to : g[v]) {
    if (u[to] == 1) continue;
    int y = m[make_pair(to, v)];
    if (y)
      dp[to] = (n - cnt[to]);
    else
      dp[to] = (dp[v] + d[v] - d[to]);
    df(to);
  }
}
bool ch(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(NULL);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    g[u].push_back(v);
    g[v].push_back(u);
    m[make_pair(u, v)] = m[make_pair(v, u)] = ch(c);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) u[i] = 0;
  df(1);
  long long u = 0;
  for (int i = 1; i <= n; i++) {
    long long o = dp[i] + d[i];
    u += (o * (o - 1));
  }
  cout << u;
  return 0;
}
