#include <bits/stdc++.h>
using namespace std;
const int THRES = 400;
const int MAX = 300000;
const int INF = (int)1E9;
const long long LINF = (long long)1E18;
const long double PI = acos(-1.0);
const long double EPS = 1E-9;
template <typename T>
T gcd(T a, T b) {
  return (b == 0) ? abs(a) : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <typename T>
inline T mod(T a, T b) {
  a %= b;
  if (a < 0) a += b;
  return a;
}
template <typename T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
inline T gmax(T a, T b) {
  return (a > b ? a : b);
}
template <typename T>
inline T gmin(T a, T b) {
  return (a < b ? a : b);
}
template <typename T>
inline string toString(T x) {
  ostringstream oss;
  oss << x;
  return oss.str();
}
inline long long toInt(const string& st) {
  istringstream iss(st);
  long long x;
  iss >> x;
  return x;
}
inline long double toDouble(const string& st) {
  istringstream iss(st);
  long double x;
  iss >> x;
  return x;
}
inline string toLower(string st) {
  for (int i = 0; i < (((int)(st).size())); i++) st[i] = tolower(st[i]);
  return st;
}
inline string toUpper(string st) {
  for (int i = 0; i < (((int)(st).size())); i++) st[i] = toupper(st[i]);
  return st;
}
bool LOCAL = false, OUTPUT = true;
const char DEBUG_PARAM[] = "__LOCAL_TESTING";
const char IN[] = "_.in";
const char OUT[] = "_.out";
inline void init();
inline void run();
int ntest = 0, test;
int main(int argc, char* argv[]) {
  if (argc > 1 && strcmp(argv[1], DEBUG_PARAM) == 0) {
    LOCAL = true;
    freopen(IN, "r", stdin);
  }
  init();
  if (ntest == 0) {
    cerr << "ntest = ?" << endl;
    return 0;
  }
  for (test = 1; test <= ntest; ++test) {
    run();
  }
  return 0;
}
inline void stop() { ntest = test - 1; }
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
inline void init() {
  OUTPUT = true;
  ntest = 1;
}
const int MAXN = 100000 + 5;
int f[MAXN], d[MAXN], g[MAXN], n;
vector<int> a[MAXN], b[MAXN];
bool lucky(int a) {
  if (a == 0) return false;
  while (a > 0) {
    if (a % 10 != 4 && a % 10 != 7) return false;
    a /= 10;
  }
  return true;
}
void calcFD(int i, int l) {
  f[i] = 0;
  d[i] = 1;
  for (int k = 0; k < (((int)(a[i]).size())); k++) {
    int j = a[i][k];
    if (j == l) continue;
    calcFD(j, i);
    d[i] += d[j];
    if (lucky(b[i][k]))
      f[i] += d[j];
    else
      f[i] += f[j];
  }
}
void calcG(int i, int l) {
  for (int k = 0; k < (((int)(a[i]).size())); k++) {
    int j = a[i][k];
    if (j == l) continue;
    g[j] = f[j];
    if (lucky(b[i][k]))
      g[j] += n - d[j];
    else
      g[j] = g[i];
    calcG(j, i);
  }
}
inline void run() {
  cin >> n;
  for (int i = 0; i < (n - 1); i++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--, y--;
    a[x].push_back(y);
    b[x].push_back(w);
    a[y].push_back(x);
    b[y].push_back(w);
  }
  calcFD(0, -1);
  g[0] = f[0];
  calcG(0, -1);
  long long ret = 0;
  for (int i = 0; i < (n); i++) ret += 1LL * g[i] * g[i] - g[i];
  cout << ret;
}
