#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
  return x * x;
}
const int maxn = 218 * 1000;
vector<pair<int, int> > g[maxn];
long long d1[maxn];
long long d2[maxn];
void dfs1(int x, int p = -1) {
  d1[x] = 1;
  for (int i = 0; i < ((int)(g[x]).size()); ++i) {
    if (g[x][i].first == p) continue;
    dfs1(g[x][i].first, x);
    d1[x] += d1[g[x][i].first];
  }
}
void dfs2(int x, int p = -1) {
  d2[x] = 0;
  for (int i = 0; i < ((int)(g[x]).size()); ++i) {
    if (g[x][i].first == p) continue;
    dfs2(g[x][i].first, x);
    if (g[x][i].second == 1)
      d2[x] += d1[g[x][i].first];
    else
      d2[x] += d2[g[x][i].first];
  }
}
long long res;
long long total;
void dfs(int x, int p = -1, long long up = 0) {
  long long cur = up;
  cur += d2[x];
  res += (cur * (cur - 1LL));
  for (int i = 0; i < ((int)(g[x]).size()); ++i) {
    if (g[x][i].first == p) continue;
    long long add;
    if (g[x][i].second == 1)
      add = total - d1[g[x][i].first];
    else
      add = cur - d2[g[x][i].first];
    dfs(g[x][i].first, x, add);
  }
}
int main() {
  vector<long long> v;
  for (int i = 1; i <= 12; ++i) {
    for (int mask = 0; mask < (1 << i); ++mask) {
      long long x = 0;
      for (int j = 0; j < i; ++j) {
        x *= 10;
        if ((mask >> j) & 1)
          x += 7;
        else
          x += 4;
      }
      v.push_back(x);
    }
  }
  sort((v).begin(), (v).end());
  v.erase(unique((v).begin(), (v).end()), v.end());
  int n;
  cin >> n;
  total = n;
  for (int i = 0; i < n - 1; ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    --x, --y;
    z = binary_search((v).begin(), (v).end(), (long long)z) ? 1 : 0;
    g[x].push_back(pair<int, int>(y, z));
    g[y].push_back(pair<int, int>(x, z));
  }
  memset(d1, -1, sizeof(d1));
  dfs1(0);
  memset(d2, -1, sizeof(d2));
  dfs2(0);
  res = 0;
  dfs(0);
  cout << res << "\n";
  return 0;
}
