#include <bits/stdc++.h>
using namespace std;
bool isLucky(int number) {
  int ok = 1;
  do {
    int digit = number % 10;
    if (digit != 4 && digit != 7) {
      ok = 0;
      break;
    }
    number /= 10;
  } while (number);
  return ok;
}
void countVertex(vector<int>& answer, const vector<vector<int> >& graph) {
  int n = graph.size();
  vector<int> visited(n, 0);
  int nVisited = 0;
  for (int start = 0; start < n; ++start) {
    if (visited[start]) continue;
    queue<int> q;
    q.push(start);
    int nVertices = 0;
    while (!q.empty()) {
      ++visited[q.front()];
      ++nVisited;
      ++nVertices;
      for (vector<int>::const_iterator p = graph[q.front()].begin();
           p != graph[q.front()].end(); ++p) {
        if (!visited[*p]) {
          q.push(*p);
        }
      }
      q.pop();
    }
    answer.push_back(nVertices);
  }
}
int main() {
  clock_t t1, t2;
  int n;
  t1 = clock();
  cin >> n;
  vector<vector<int> > graph(n);
  for (int i = 0; i < n - 1; ++i) {
    int from, to, weight;
    cin >> from >> to >> weight;
    --from;
    --to;
    if (!isLucky(weight)) {
      graph[from].push_back(to);
      graph[to].push_back(from);
    }
  }
  t2 = clock();
  cerr << "init time: " << (t2 - t1) / (double)CLOCKS_PER_SEC << endl;
  vector<int> vertexInConnectedness;
  t1 = clock();
  countVertex(vertexInConnectedness, graph);
  t2 = clock();
  cerr << "bfs time: " << (t2 - t1) / (double)CLOCKS_PER_SEC << endl;
  vector<int> other(vertexInConnectedness.size());
  t1 = clock();
  for (size_t i = 0; i < other.size(); ++i)
    other[i] = n - vertexInConnectedness[i];
  long long res = 0;
  for (size_t i = 0; i < other.size(); ++i) {
    res += vertexInConnectedness[i] * (long long)other[i] * (other[i] - 1);
  }
  t2 = clock();
  cerr << "answ time: " << (t2 - t1) / (double)CLOCKS_PER_SEC << endl;
  cout << res << endl;
  return 0;
}
