#include <bits/stdc++.h>
using namespace std;
vector<pair<int, long long int> > v[100005];
pair<int, int> A[100005];
bool ch(long long int n) {
  bool ans = true;
  while (n != 0) {
    if (n % 10 != 7 && n % 10 != 4) ans = false;
    n = n / 10;
  }
  return ans;
}
pair<int, int> dfs(int in, int p) {
  pair<int, int> ans = pair<int, int>(0, 0);
  for (int i = 0; i < v[in].size(); i++) {
    if (v[in][i].first == p) continue;
    pair<int, int> an = dfs(v[in][i].first, in);
    if (ch(v[in][i].second)) {
      ans.first += (an.second) + 1;
      ans.second += (an.second) + 1;
    } else {
      ans.first += an.first;
      ans.second += (an.second) + 1;
    }
  }
  A[in] = ans;
  return ans;
}
void dfs1(int in, int p, long long int w) {
  if (in != 0) {
    if (ch(w)) {
      int t = A[p].second - A[in].second;
      A[in].second += t;
      A[in].first += t;
    } else {
      int t = A[p].second - A[in].second;
      int ft = A[p].first - A[in].first;
      A[in].second += t;
      A[in].first += ft;
    }
  }
  for (int i = 0; i < v[in].size(); i++) {
    if (v[in][i].first == p) continue;
    dfs1(v[in][i].first, in, v[in][i].second);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    long long int w;
    cin >> x >> y >> w;
    v[x - 1].push_back(pair<int, long long int>(y - 1, w));
    v[y - 1].push_back(pair<int, long long int>(x - 1, w));
  }
  pair<int, int> tmp = dfs(0, -1);
  dfs1(0, -1, 0);
  long long int sum = 0;
  for (int i = 0; i < n; i++) {
    long long int ans = A[i].first;
    sum += (ans) * (ans - 1);
  }
  cout << sum << "\n";
  return 0;
}
