#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int n;
vector<pair<int, bool> > out[MAXN];
int size[MAXN];
long long f[MAXN], g[MAXN];
bool is_lucky(int n) {
  while (n > 0) {
    int t = n % 10;
    if (t != 4 && t != 7) return false;
    n /= 10;
  }
  return true;
}
void dfs_size(int v, int p = -1) {
  size[v] = 1;
  for (int i = 0; i < out[v].size(); i++) {
    int to = out[v][i].first;
    if (to != p) {
      dfs_size(to, v);
      size[v] += size[to];
    }
  }
}
void dfs_f(int v, int p = -1) {
  f[v] = 0;
  for (int i = 0; i < out[v].size(); i++)
    if (out[v][i].first != p) {
      pair<int, bool> to = out[v][i];
      dfs_f(to.first, v);
      if (to.second)
        f[v] += size[to.first];
      else
        f[v] += f[to.first];
    }
}
void dfs_g(int v, int p = -1, bool isl = false) {
  g[v] = 0;
  if (p != -1) {
    if (isl) {
      g[v] += size[0] - size[v];
    } else {
      g[v] += g[p] + f[p] - f[v];
    }
  }
  for (int i = 0; i < out[v].size(); i++) {
    pair<int, bool> to = out[v][i];
    if (to.first != p) dfs_g(to.first, v, to.second);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    bool t = is_lucky(w);
    out[u].push_back(make_pair(v, t));
    out[v].push_back(make_pair(u, t));
  }
  dfs_size(0);
  dfs_f(0);
  dfs_g(0);
  long long ans = 0;
  for (int i = 0; i < n; i++)
    ans += f[i] * (f[i] - 1) + g[i] * (g[i] - 1) + 2 * f[i] * g[i];
  cout << ans << endl;
}
