#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
vector<pair<int, int> > E[maxn];
int n;
bool ok(int x) {
  if (x == 0) return false;
  while (x) {
    int r = x % 10;
    x /= 10;
    if (r != 4 && r != 7) return false;
  }
  return true;
}
long long sz[maxn];
long long ct[maxn];
void gao(int x, int fa) {
  int i;
  sz[x] = 1;
  for (i = 0; i < E[x].size(); ++i) {
    if (E[x][i].first != fa) {
      gao(E[x][i].first, x);
      sz[x] += sz[E[x][i].first];
    }
  }
}
void gao1(int x, int fa) {
  int i;
  ct[x] = 0;
  for (i = 0; i < E[x].size(); ++i) {
    if (E[x][i].first != fa) {
      gao1(E[x][i].first, x);
      ct[x] += (E[x][i].second ? sz[E[x][i].first] : ct[E[x][i].first]);
    }
  }
}
long long ans;
void gao2(int x, int fa, long long cnt) {
  long long nowc = cnt;
  int i;
  ans += ct[x] * (ct[x] - 1);
  ans += cnt * (cnt - 1);
  ans += ct[x] * cnt * 2;
  for (i = 0; i < E[x].size(); ++i) {
    if (E[x][i].first != fa) {
      nowc += (E[x][i].second ? sz[E[x][i].first] : ct[E[x][i].first]);
    }
  }
  for (i = 0; i < E[x].size(); ++i) {
    if (E[x][i].first != fa) {
      if (E[x][i].second)
        gao2(E[x][i].first, x, n - sz[E[x][i].first]);
      else
        gao2(E[x][i].first, x, nowc - ct[E[x][i].first]);
    }
  }
}
void get() {
  int i;
  for (i = 1; i <= n; ++i) E[i].clear();
  for (i = 1; i < n; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    E[a].push_back(make_pair(b, ok(c)));
    E[b].push_back(make_pair(a, ok(c)));
  }
}
void work() {
  gao(1, 0);
  gao1(1, 0);
  ans = 0;
  gao2(1, 0, 0);
  int i;
  cout << ans << endl;
}
int main() {
  while (cin >> n) {
    get();
    work();
  }
  return 0;
}
