#include <bits/stdc++.h>
using namespace std;
long long n;
vector<pair<long long, long long>> adj[100009];
long long x, y, z;
int mark[100009];
vector<long long> v1;
vector<long long> v2;
vector<long long> v3;
long long ans1[100009];
long long ans2[100009];
long long bach[100009];
long long bach2[100009];
bool islucky(long long A) {
  while (A > 0) {
    if (A % 10 != 4 && A % 10 != 7) {
      return 0;
    }
    A /= 10;
  }
  return 1;
}
void dfs(long long A) {
  mark[A] = 1;
  for (int i = 0; i < adj[A].size(); i++) {
    if (mark[adj[A][i].first] == 0) {
      v1.push_back(A);
      v2.push_back(adj[A][i].first);
      v3.push_back(adj[A][i].second);
      dfs(adj[A][i].first);
      bach[A] += bach[adj[A][i].first] + 1;
    }
  }
}
void dfs2(long long A) {
  mark[A] = 2;
  for (int i = 0; i < adj[A].size(); i++) {
    if (mark[adj[A][i].first] == 1) {
      if (adj[A][i].second == 0) {
        ans2[adj[A][i].first] = ans2[A] + ans1[A] - ans1[adj[A][i].first];
      } else {
        ans2[adj[A][i].first] = n - bach[adj[A][i].first] - 1;
      }
      dfs2(adj[A][i].first);
    }
  }
}
long long ans;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    adj[x].push_back({y, islucky(z)});
    adj[y].push_back({x, islucky(z)});
  }
  dfs(0);
  for (int i = v1.size() - 1; i >= 0; i--) {
    if (v3[i] == 0) {
      ans1[v1[i]] += ans1[v2[i]];
    } else {
      ans1[v1[i]] += bach[v2[i]] + 1;
    }
  }
  dfs2(0);
  for (int i = 0; i < n; i++) {
    ans += ans1[i] * ans2[i] * 2 + ans1[i] * (ans1[i] - 1) +
           ans2[i] * (ans2[i] - 1);
  }
  cout << ans << endl;
}
