#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long double PI = 2 * acos(0.0);
const vector<long long int> dx = {1, -1, 0, 0};
const vector<long long int> dy = {0, 0, 1, -1};
vector<long long int> ga(long long int n, bool oneIndexed = false) {
  vector<long long int> a;
  if (oneIndexed) a.push_back(0ll);
  for (long long int i = 0; i < n; i++) {
    long long int p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
vector<string> gas(unsigned long long int n, bool oneIndexed = false) {
  vector<string> a;
  if (oneIndexed) a.push_back("");
  for (unsigned long long int i = 0; i < n; i++) {
    string p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
template <typename T, typename A>
void pa(vector<T, A> const &a, long long int begin = 0,
        long long int end = -1) {
  if (end == -1) end = (long long int)a.size() - 1;
  for (long long int i = begin; i <= end; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
template <typename T, typename A>
void papair(vector<T, A> const &a, long long int begin = 0,
            long long int end = -1) {
  if (end == -1) end = (long long int)a.size() - 1;
  for (long long int i = begin; i <= end; i++) {
    cout << a[i].first << " " << a[i].second << "\n";
  }
}
long long int lucky(long long int c) {
  long long int f = 0;
  while (c > 0) {
    if (c % 10 != 4 && c % 10 != 7) {
      f = 1;
      break;
    }
    c /= 10;
  }
  return f;
}
long long int dfs(vector<vector<long long int> > &adjList, long long int curr,
                  long long int prev, vector<long long int> &visited) {
  visited[curr] = 1;
  long long int s = 0;
  for (auto neigh : adjList[curr]) {
    if (neigh == prev) continue;
    if (visited[neigh] == 0) {
      s += dfs(adjList, neigh, curr, visited);
    } else if (visited[neigh] == 1) {
    }
  }
  visited[curr] = 2;
  return 1 + s;
}
void solve() {
  long long int n;
  cin >> n;
  vector<vector<long long int> > adjList(n + 1);
  for (long long int i = 0; i < n - 1; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    if (lucky(c)) {
      adjList[a].push_back(b);
      adjList[b].push_back(a);
    }
  }
  vector<long long int> visited(n + 1, 0);
  long long int sum = 0;
  for (long long int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      long long int cnt = dfs(adjList, i, -1, visited);
      long long int c2 = n - cnt;
      sum += cnt * ((c2) * (c2 - 1)) / 2;
    }
  }
  cout << sum * 2;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
