#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 2e5 + 5;
const long long mod = 1e9 + 7;
const double PI = acos(-1.0);
long long po(long long a, long long b, long long mod) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (a == 0) {
    return b;
  } else {
    return gcd(b % a, a);
  }
}
bool is(int x) {
  if (x == 0) return false;
  while (x > 0) {
    int p = x % 10;
    if (p == 4 || p == 7) {
      x /= 10;
    } else {
      return false;
    }
  }
  return true;
}
int n;
long long up[maxn], down[maxn], cnt[maxn];
vector<pair<int, int>> edge[maxn];
void dfs1(int x, int fa) {
  cnt[x] = 1;
  for (int i = 0; i < edge[x].size(); i++) {
    int k = edge[x][i].first;
    if (k == fa) continue;
    dfs1(k, x);
    int path = edge[x][i].second;
    if (is(path)) {
      down[x] += cnt[k];
    } else {
      down[x] += down[k];
    }
    cnt[x] += cnt[k];
  }
}
void dfs2(int x, int fa) {
  for (int i = 0; i < edge[x].size(); i++) {
    int k = edge[x][i].first;
    if (k == fa) continue;
    int path = edge[x][i].second;
    if (is(path)) {
      up[k] += n - (cnt[k]);
    } else {
      up[k] += up[x] + down[x] - down[k];
    }
    dfs2(k, x);
  }
}
void solve() {
  scanf("%d", &(n));
  for (int i = 1; i <= n - 1; i++) {
    int x, y, z;
    scanf("%d", &(x)), scanf("%d", &(y)), scanf("%d", &(z));
    edge[x].push_back(make_pair(y, z));
    edge[y].push_back(make_pair(x, z));
  }
  dfs1(1, -1);
  dfs2(1, -1);
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    long long k = up[i] + down[i];
    res += k * (k - 1);
  }
  printf("%lld", res);
}
int main() {
  solve();
  return 0;
}
