#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 100 * 1000 + 9;
int n;
int nChild[maxN], f[maxN], fup[maxN];
vector<pair<int, bool> > g[maxN];
bool lucky(int x) {
  while (x > 0) {
    int dig = x % 10;
    if (dig != 4 && dig != 7) {
      return false;
    }
    x /= 10;
  }
  return true;
}
void dfs0(int u, int par) {
  nChild[u]++;
  int curSum = 0;
  for (int i = 0, L = g[u].size(); i < L; i++) {
    int v = g[u][i].first;
    bool w = g[u][i].second;
    if (v == par) {
      continue;
    }
    dfs0(v, u);
    nChild[u] += nChild[v];
    if (w) {
      f[u] += nChild[v];
    } else {
      f[u] += f[v];
    }
    curSum += f[v];
  }
}
void dfs1(int u, int par) {
  for (int i = 0, L = g[u].size(); i < L; i++) {
    int v = g[u][i].first;
    bool w = g[u][i].second;
    if (v == par) {
      continue;
    }
    if (w) {
      fup[v] += (n - nChild[v]);
    } else {
      fup[v] += fup[u];
      fup[v] += (f[u] - f[v]);
    }
    dfs1(v, u);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    w = lucky(w);
    g[u].push_back(make_pair(v, w));
    g[v].push_back(make_pair(u, w));
  }
  dfs0(1, 0);
  dfs1(1, 0);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    long long tot = (long long)f[i] + (long long)fup[i];
    ans += tot * (tot - 1);
  }
  cout << ans << '\n';
  return 0;
}
