#include <bits/stdc++.h>
using namespace std;
struct Arc {
  int dst;
  int weight;
};
vector<Arc> l[100010];
int numVert[100010] = {};
int numSons[100010] = {};
set<int> lucky;
int DFS1(int v, int p = -1) {
  for (int i = 0; i < l[v].size(); i++) {
    if (l[v][i].dst == p) continue;
    if (lucky.count(l[v][i].weight)) {
      numVert[v] += numSons[l[v][i].dst] + 1;
      DFS1(l[v][i].dst, v);
    } else {
      DFS1(l[v][i].dst, v);
      numVert[v] += numVert[l[v][i].dst];
    }
  }
  return numVert[v];
}
int DFS2(int v, int p = -1) {
  for (int i = 0; i < l[v].size(); i++) {
    if (l[v][i].dst == p) continue;
    numSons[v] += DFS2(l[v][i].dst, v) + 1;
  }
  return numSons[v];
}
long long ans = 0;
void DFS3(int v, int p = -1, long long up = 0, long long upsons = 0) {
  long long x = numVert[v] + up;
  ans += x * (x - 1);
  for (int i = 0; i < l[v].size(); i++) {
    if (l[v][i].dst == p) continue;
    if (lucky.count(l[v][i].weight)) {
      DFS3(l[v][i].dst, v, numSons[v] + 1 - (numSons[l[v][i].dst] + 1) + upsons,
           numSons[v] - numSons[l[v][i].dst] + upsons);
    } else {
      DFS3(l[v][i].dst, v, numVert[v] - numVert[l[v][i].dst] + up,
           numSons[v] - numSons[l[v][i].dst] + upsons);
    }
  }
}
int main() {
  int n;
  cin >> n;
  for (int len = 1; len <= 9; len++) {
    for (int i = 1; i <= 1024; i++) {
      int number = 0;
      for (int u = 0; u < len; u++) {
        if (i & (1 << u)) {
          number = number * 10 + 4;
        } else {
          number = number * 10 + 7;
        }
      }
      lucky.insert(number);
    }
  }
  for (int i = 1; i < n; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    Arc tmp;
    tmp.dst = b;
    tmp.weight = w;
    l[a].push_back(tmp);
    tmp.dst = a;
    l[b].push_back(tmp);
  }
  DFS2(1);
  DFS1(1);
  DFS3(1);
  cout << ans << endl;
  return 0;
}
