#include <bits/stdc++.h>
using namespace std;
long long add(long long a, long long b) {
  return (a % 1000000007 + b % 1000000007 +
          ((8000000000000000064LL) / 1000000007) * 1000000007) %
         1000000007;
}
long long sub(long long a, long long b) {
  return (a % 1000000007 - b % 1000000007 +
          ((8000000000000000064LL) / 1000000007) * 1000000007) %
         1000000007;
}
long long mul(long long a, long long b) {
  return ((a % 1000000007) * (b % 1000000007) +
          ((8000000000000000064LL) / 1000000007) * 1000000007) %
         1000000007;
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long fact[1000007] = {0};
long long expo(long long x, long long y) {
  long long res = 1;
  x = x % 1000000007;
  while (y > 0) {
    if (y & 1) res = (1ll * res * x) % 1000000007;
    y = y >> 1;
    x = (1ll * x * x) % 1000000007;
  }
  return res;
}
void facto() {
  fact[0] = 1;
  fact[1] = 1;
  for (long long i = 2; i < 1000007; i++)
    fact[i] = (fact[i - 1] * i) % 1000000007;
}
long long ncr(long long n, long long r) {
  long long res = 1;
  res = fact[n];
  res = (res * (expo(fact[r], 1000000007 - 2))) % 1000000007;
  res = (res * (expo(fact[n - r], 1000000007 - 2))) % 1000000007;
  return res;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int const N = 1e3 + 9;
long long const INF = 2e18 + 5;
long long dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
long long dy[8] = {1, -1, 0, 0, -1, 1, -1, 1};
char dr[4] = {'R', 'L', 'D', 'U'};
long long n, ans;
vector<vector<pair<long long, long long>>> v;
vector<long long> child, dp;
bool is_lucky(long long c) {
  while (c) {
    if (c % 10 != 4 && c % 10 != 7) {
      return 0;
    }
    c /= 10;
  }
  return 1;
}
void dfs(long long x, long long p) {
  child[x] = 1;
  for (auto i : v[x]) {
    if (i.first == p) continue;
    dfs(i.first, x);
    child[x] += child[i.first];
    if (i.second) {
      dp[x] += child[i.first];
    } else {
      dp[x] += dp[i.first];
    }
  }
}
void re_root(long long x, long long p) {
  if (dp[x] >= 2) {
    ans += (dp[x] * (dp[x] - 1));
  }
  for (auto i : v[x]) {
    if (i.first == p) continue;
    if (i.second) {
      dp[i.first] += n - child[i.first];
    } else {
      dp[x] -= dp[i.first];
      dp[i.first] += dp[x];
    }
    re_root(i.first, x);
    if (i.second) {
      dp[i.first] -= n + child[i.first];
    } else {
      dp[i.first] -= dp[x];
      dp[x] += dp[i.first];
    }
  }
}
void solve() {
  ans = 0;
  cin >> n;
  v = vector<vector<pair<long long, long long>>>(n + 1);
  for (long long i = 0; i < n - 1; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    c = is_lucky(c);
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  child = vector<long long>(n + 1, 0);
  dp = vector<long long>(n + 1, 0);
  dfs(1, -1);
  re_root(1, -1);
  cout << ans << "\n";
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t start = clock();
  facto();
  { solve(); }
  double duration = (clock() - start) / (double)CLOCKS_PER_SEC;
  cerr << duration << 's' << endl;
}
