#include <bits/stdc++.h>
using namespace std;
void mytimer(string task) {}
void ext(int c) {}
const int lim = 110000;
vector<pair<int, bool> > gr[lim];
int sz[lim];
int down[lim];
int up[lim];
int n;
void getdown(int v, int par) {
  sz[v] = 1;
  for (int i = 0; i < (int)gr[v].size(); ++i) {
    int p = gr[v][i].first;
    if (p == par) continue;
    getdown(p, v);
    if (gr[v][i].second)
      down[v] += sz[p];
    else
      down[v] += down[p];
    sz[v] += sz[p];
  }
}
void getup(int v, int u, int par) {
  up[v] = u;
  for (int i = 0; i < (int)gr[v].size(); ++i) {
    int p = gr[v][i].first;
    if (p == par) continue;
    if (gr[v][i].second)
      getup(p, n - sz[p], v);
    else
      getup(p, u + down[v] - down[p], v);
  }
}
bool lucky(int a) {
  while (a) {
    int t = a % 10;
    if (t != 4 && t != 7) return false;
    a /= 10;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < (n - 1); ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    bool l = lucky(c);
    gr[a].push_back(make_pair(b, l));
    gr[b].push_back(make_pair(a, l));
  }
  getdown(0, -1);
  getup(0, 0, -1);
  long long res = 0;
  for (int i = 0; i < (n); ++i) {
    long long v = down[i] + up[i];
    res += v * (v - 1);
  }
  cout << res << flush;
  return 0;
}
