#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<pair<int, int> > g[N];
struct edge {
  int from, to;
  bool lucky;
};
edge edges[N];
int n, lucky[N], sz[N];
long long ans = 0;
void dfs(int node, int parent = -1) {
  sz[node] = 1;
  for (auto child : g[node])
    if (child.first != parent) {
      dfs(child.first, node);
      sz[node] += sz[child.first];
      if (edges[child.second].lucky)
        lucky[node] += sz[child.first];
      else
        lucky[node] += lucky[child.first];
    }
}
void shift(int from, int to, edge x, bool ray7) {
  if (ray7) {
    if (x.lucky)
      lucky[from] -= sz[to];
    else
      lucky[from] -= lucky[to];
    sz[from] -= sz[to];
    sz[to] = n;
    if (x.lucky)
      lucky[to] += sz[from];
    else
      lucky[to] += lucky[from];
  } else {
    if (x.lucky)
      lucky[to] -= sz[from];
    else
      lucky[to] -= lucky[from];
    sz[to] -= sz[from];
    sz[from] = n;
    if (x.lucky)
      lucky[from] += sz[to];
    else
      lucky[from] += lucky[to];
  }
}
void solve(int node, int parent = -1) {
  ans += 1LL * lucky[node] * (lucky[node] - 1);
  for (auto child : g[node])
    if (child.first != parent) {
      shift(node, child.first, edges[child.second], 1);
      solve(child.first, node);
      shift(node, child.first, edges[child.second], 0);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, y;
    long long z;
    scanf("%d%d%I64d", &x, &y, &z);
    bool l = 1;
    while (z != 0) {
      if (z % 10 != 4 && z % 10 != 7) l = 0;
      z /= 10;
    }
    edges[i] = {x, y, l};
    g[x].push_back({y, i});
    g[y].push_back({x, i});
  }
  dfs(1);
  solve(1);
  cout << ans;
  return 0;
}
