#include <bits/stdc++.h>
using namespace std;
int n, a, b, w, c[200005], dp1[200005], dp2[200005];
long long ans;
vector<vector<pair<int, int>>> adjList;
bitset<200005> visited;
bool lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) {
      return 0;
    }
    x /= 10;
  }
  return 1;
}
void dfs1(int u) {
  visited[u] = 1;
  c[u] = 1;
  for (int i = 0; i < adjList[u].size(); i++) {
    int v = adjList[u][i].first;
    if (!visited[v]) {
      dfs1(v);
      c[u] += c[v];
    }
  }
}
void dfs2(int u) {
  visited[u] = 1;
  for (int i = 0; i < adjList[u].size(); i++) {
    pair<int, int> v = adjList[u][i];
    if (!visited[v.first]) {
      dfs2(v.first);
      if (lucky(v.second)) {
        dp1[u] += c[v.first];
      } else {
        dp1[u] += dp1[v.first];
      }
    }
  }
}
void dfs3(int u) {
  visited[u] = 1;
  for (int i = 0; i < adjList[u].size(); i++) {
    pair<int, int> v = adjList[u][i];
    if (!visited[v.first]) {
      if (lucky(v.second)) {
        dp2[v.first] += (c[1] - c[v.first]);
      } else {
        dp2[v.first] += (dp2[u] + dp1[u] - dp1[v.first]);
      }
      dfs3(v.first);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  adjList.resize(n + 5);
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b >> w;
    adjList[a].push_back({b, w});
    adjList[b].push_back({a, w});
  }
  dfs1(1);
  visited.reset();
  dfs2(1);
  visited.reset();
  dfs3(1);
  for (int i = 1; i <= n; i++) {
    long long pairs = dp1[i] + dp2[i];
    ans += (pairs * (pairs - 1));
  }
  cout << ans << '\n';
  return 0;
}
