#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
vector<pair<int, bool> > graph[N], tree[N];
bool visited[N];
int n;
bool isLucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int treeSize[N];
void buildTree(int t) {
  visited[t] = true;
  treeSize[t] = 1;
  for (vector<pair<int, bool> >::iterator iter = graph[t].begin();
       iter != graph[t].end(); iter++) {
    if (!visited[iter->first]) {
      tree[t].push_back(*iter);
      buildTree(iter->first);
      treeSize[t] += treeSize[iter->first];
    }
  }
}
int treeCount[N];
void scanRootedTree(int t) {
  for (vector<pair<int, bool> >::iterator iter = tree[t].begin();
       iter != tree[t].end(); iter++) {
    scanRootedTree(iter->first);
    if (iter->second)
      treeCount[t] += treeSize[iter->first];
    else
      treeCount[t] += treeCount[iter->first];
  }
}
void scanUnrootedTree(int t, int count) {
  treeCount[t] += count;
  for (vector<pair<int, bool> >::iterator iter = tree[t].begin();
       iter != tree[t].end(); iter++) {
    int tcount = 0;
    if (iter->second)
      tcount = treeSize[1] - treeSize[iter->first];
    else
      tcount = treeCount[t] - treeCount[iter->first];
    scanUnrootedTree(iter->first, tcount);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y, w;
    scanf("%d%d%d", &x, &y, &w);
    bool flag = isLucky(w);
    graph[x].push_back(pair<int, bool>(y, flag));
    graph[y].push_back(pair<int, bool>(x, flag));
  }
  memset(visited, false, sizeof(visited));
  memset(treeSize, 0, sizeof(treeSize));
  buildTree(1);
  memset(treeCount, 0, sizeof(treeCount));
  scanRootedTree(1);
  scanUnrootedTree(1, 0);
  long long ret = 0;
  for (int i = 1; i <= n; i++) {
    ret += treeCount[i] * (long long)(treeCount[i] - 1);
  }
  cout << ret << endl;
}
