#include <bits/stdc++.h>
using namespace std;
long long down[100004];
long long up[100004];
long long number[100004];
class Graph {
  long long V;
  list<pair<long long, long long> > *adj;

 public:
  Graph(long long V);
  void addEdge(long long u, long long v, long long w);
  void DFSUtil(long long v, bool visited[]);
  void DFS(long long v, long long parent, long long w);
  bool check(long long w);
};
Graph::Graph(long long V) {
  this->V = V;
  adj = new list<pair<long long, long long> >[V];
}
void Graph::addEdge(long long u, long long v, long long w) {
  adj[u].push_back({v, w});
  adj[v].push_back({u, w});
}
bool Graph::check(long long w) {
  while (w) {
    long long temp = w - (w / 10) * 10;
    if (temp != 4 && temp != 7) {
      return false;
    }
    w = w / 10;
  }
  return true;
}
void Graph::DFSUtil(long long v, bool visited[]) {
  visited[v] = true;
  list<pair<long long, long long> >::iterator i;
  for (i = adj[v].begin(); i != adj[v].end(); ++i) {
    if (!visited[i->first]) {
      DFSUtil(i->first, visited);
      number[v] += number[i->first];
      if (check(i->second))
        down[v] += number[i->first];
      else {
        down[v] += down[i->first];
      }
    }
  }
}
void Graph::DFS(long long v, long long parent, long long w) {
  if (parent == -1) {
    up[v] = 0;
  } else {
    if (check(w)) {
      up[v] = number[0] - number[v];
    } else {
      up[v] = down[parent] - down[v] + up[parent];
    }
  }
  list<pair<long long, long long> >::iterator i;
  for (i = adj[v].begin(); i != adj[v].end(); ++i) {
    if (i->first != parent) {
      DFS(i->first, v, i->second);
    }
  }
}
int main() {
  long long n;
  cin >> n;
  Graph g(n);
  for (long long i = 0; i < n - 1; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    g.addEdge(u, v, w);
  }
  bool visited[n];
  for (long long i = 0; i < n; i++) {
    visited[i] = false;
    number[i] = 1;
  }
  g.DFSUtil(0, visited);
  g.DFS(0, -1, -1);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    ans += 2 * down[i] * up[i];
    long long a = max(0ll, down[i] * (down[i] - 1));
    long long b = max(0ll, up[i] * (up[i] - 1));
    ans += (a + b);
  }
  cout << ans << endl;
}
