#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 2e9;
const long long N = 1e5 + 15;
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, long long n) {
  stringstream s;
  s << "[";
  for (long long i = 0; i < n - 1; i++) s << arr[i] << ",";
  s << arr[n - 1] << "]";
  return s.str();
}
inline void __evars_begin(long long line) { cerr << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
  cerr << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T* val) {
  cerr << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
  cerr << val;
}
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  __evars_out_var(a);
  cerr << "; ";
  __evars(++it, args...);
}
vector<pair<long long, long long>> adj[N];
long long f[N], sz[N], n;
void dfs(long long x, long long p) {
  sz[x]++;
  for (auto& i : adj[x]) {
    if (i.first == p) continue;
    dfs(i.first, x);
    sz[x] += sz[i.first];
    if (i.second)
      f[x] += sz[i.first];
    else
      f[x] += f[i.first];
  }
}
void dfs(long long x, long long p, long long sg) {
  if (p != -1) {
    if (sg)
      f[x] += n - sz[x];
    else
      f[x] = f[p];
  }
  for (auto& i : adj[x]) {
    if (i.first == p) continue;
    dfs(i.first, x, i.second);
  }
}
long long chk(long long x) {
  while (x) {
    long long r = x % 10;
    if (r != 4 && r != 7) return 0;
    x /= 10;
  }
  return 1;
}
void kudne() {
  cin >> n;
  for (long long i = 1; i < n; ++i) {
    long long x, y, z;
    cin >> x >> y >> z;
    adj[x].push_back({y, chk(z)});
    adj[y].push_back({x, chk(z)});
  }
  dfs(1, -1);
  dfs(1, -1, 0);
  long long ans = 0;
  for (long long i = 1; i <= n; ++i) {
    ans += (f[i] * (f[i] - 1));
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tc = 1;
  for (long long i = 1; i <= tc; ++i) {
    kudne();
    if (i < tc) cout << "\n";
  }
  return 0;
}
