#include <bits/stdc++.h>
using namespace std;
int nextInt() {
  int x = 0, p = 1;
  char ch;
  do {
    ch = getchar();
  } while (ch <= ' ');
  if (ch == '-') {
    p = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * p;
}
const int N = 100500;
const int INF = (int)1e9;
vector<pair<int, int> > g[N];
int n;
int c[N];
int dp[N];
int sz[N];
int up[N][20];
void dfs(int v, int p, int t) {
  sz[v] = 1;
  dp[v] = 0;
  c[v] = c[p] + t;
  up[v][0] = p;
  for (int i = 1; i < 20; ++i) {
    up[v][i] = up[up[v][i - 1]][i - 1];
  }
  for (size_t i = 0; i < g[v].size(); ++i) {
    int x = g[v][i].first;
    int y = g[v][i].second;
    if (x == p) continue;
    dfs(x, v, y);
    sz[v] += sz[x];
    if (y == 1) {
      dp[v] += sz[x];
    } else {
      dp[v] += dp[x];
    }
  }
}
int go(int v, int depth) {
  for (int i = 0; i < 20; ++i) {
    if ((depth >> i) & 1) {
      v = up[v][i];
    }
  }
  return v;
}
int findP(int v) {
  int l = 0, r = n + 10;
  while (l < r) {
    int mid = (l + r) / 2;
    int u = go(v, mid);
    if (c[v] == c[up[u][0]]) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return go(v, l);
}
int calc(int v) {
  int x = findP(v);
  return n - sz[x] + dp[x];
}
bool isLucky(int x) {
  while (x != 0) {
    int rem = x % 10;
    x /= 10;
    if (rem != 4 && rem != 7) {
      return false;
    }
  }
  return true;
}
int main() {
  n = nextInt();
  for (int i = 0; i < n - 1; ++i) {
    int x, y, z;
    x = nextInt();
    y = nextInt();
    z = nextInt();
    int c = isLucky(z);
    g[x].push_back(make_pair(y, c));
    g[y].push_back(make_pair(x, c));
  }
  dfs(1, 0, 1);
  long long result = 0LL;
  for (int i = 1; i <= n; ++i) {
    int x = calc(i);
    result += 1LL * x * (x - 1);
  }
  printf("%I64d\n", result);
  return 0;
}
