#include <bits/stdc++.h>
using namespace std;
bool lucky_p(int w) {
  for (; w > 0; w /= 10) {
    int ld = w % 10;
    if (ld != 4 && ld != 7) return false;
  }
  return true;
}
const int LIM = 111111;
vector<pair<bool, int> > g[LIM];
long long f[LIM], c[LIM], f_[LIM];
void dfs(int v, int p) {
  c[v] = 1;
  for (__typeof__((g[v]).begin()) it = ((g[v]).begin()); (it) != (g[v]).end();
       ++(it))
    if (it->second != p) {
      dfs(it->second, v);
      c[v] += c[it->second];
      if (it->first)
        f[v] += c[it->second];
      else
        f[v] += f[it->second];
    }
}
void dfs_(int v, int p) {
  for (__typeof__((g[v]).begin()) it = ((g[v]).begin()); (it) != (g[v]).end();
       ++(it))
    if (it->second != p) {
      if (it->first)
        f_[it->second] = c[0] - c[it->second];
      else
        f_[it->second] = f_[v] + f[v] - f[it->second];
      dfs_(it->second, v);
    }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < int(n - 1); ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    bool l = lucky_p(w);
    g[v].push_back(make_pair(l, u));
    g[u].push_back(make_pair(l, v));
  }
  dfs(0, -1);
  dfs_(0, -1);
  long long sum = 0;
  for (int v = 0; v < int(n); ++v)
    sum += f[v] * (f[v] - 1) + f_[v] * (f_[v] - 1) + 2 * f[v] * f_[v];
  cout << sum << endl;
}
