#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
const int MAXN = 1e5 + 5;
int L[MAXN];
int R[MAXN];
int cntR[MAXN];
int cntL[MAXN];
vector<pair<int, int> > way[MAXN];
int N;
inline bool islucky(int a) {
  while (a) {
    if (a % 10 != 4 && a % 10 != 7) return false;
    a /= 10;
  }
  return true;
}
int makeR(int node, int pre) {
  for (int i = 0; i < way[node].size(); i++)
    if (way[node][i].first != pre)
      cntR[node] += makeR(way[node][i].first, node);
  return ++cntR[node];
}
void makeL() {
  for (int i = 1; i <= N; i++) cntL[i] = N - cntR[i] + 1;
}
int rec(int node, int pre) {
  for (int i = 0; i < way[node].size(); i++) {
    int to = way[node][i].first;
    int c = way[node][i].second;
    if (pre != to) {
      if (c)
        R[node] += cntR[to], rec(to, node);
      else
        R[node] += rec(to, node);
    }
  }
  return R[node];
}
void f(int node, int pre) {
  for (int i = 0; i < way[node].size(); ++i) {
    int to = way[node][i].first;
    int c = way[node][i].second;
    if (to == pre) continue;
    int a1 = (c ? cntR[node] - cntR[to] : R[node] - R[to]);
    int a2 = c ? cntL[node] - 1 : L[node];
    L[to] = a1 + a2;
    f(to, node);
  }
}
int main() {
  cin >> N;
  for (int i = 1; i < N; i++) {
    int a, b, c;
    scanf(" %d %d %d", &a, &b, &c);
    way[a].push_back(pair<int, int>(b, islucky(c)));
    way[b].push_back(pair<int, int>(a, islucky(c)));
  }
  makeR(1, -1);
  makeL();
  rec(1, -1);
  f(1, -1);
  long long res = 0LL;
  for (int i = 1; i <= N; i++) {
    long long c = L[i] + R[i];
    res += c * (c - 1);
  }
  cout << res << endl;
  return 0;
}
