#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, sz[N], sum[N];
int64_t r[N][3], ans;
vector<pair<int, int>> g[N];
void dfs(int x, int f) {
  sz[x] = 1;
  for (auto &e : g[x]) {
    int y = e.first, w = e.second;
    if (y == f) continue;
    dfs(y, x), sz[x] += sz[y];
    if (w) sum[y] = sz[y], r[y][1] += r[y][0], r[y][0] = 0;
    r[x][0] += r[y][0];
    r[x][1] += r[y][1];
    sum[x] += sum[y];
  }
  for (auto &e : g[x]) {
    int y = e.first, w = e.second;
    if (y == f) continue;
    ans += sum[y] * (int64_t)(sz[x] - sz[y]) * (sz[x] - sz[y] - 1);
    ans +=
        (sz[y] - sum[y]) * (int64_t)(sum[x] - sum[y]) * (sum[x] - sum[y] - 1);
    ans += 2 * r[y][0] * (sum[x] - sum[y]);
    ans += 2 * r[y][1] * (sz[x] - sz[y]);
    r[x][0] += (sz[y] - sum[y]) * (int64_t)(sum[x] - sum[y]);
    r[x][1] += sum[y] * (int64_t)(sz[x] - sz[y]);
  }
  r[x][0] += sum[x];
  ans += sum[x] * (sum[x] - 1ll);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    int ok = 1;
    while (w) {
      if (w % 10 != 7 && w % 10 != 4) ok = 0;
      w /= 10;
    }
    g[u].push_back({v, ok});
    g[v].push_back({u, ok});
  }
  dfs(1, 0);
  printf("%lld\n", ans);
}
