#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > a, b;
int parent[100009];
int d[100009];
int ldown[100009];
int upper[1000009];
void dfs(int x) {
  int i;
  for (i = (0); i < (a[x].size()); ++i) {
    int y = a[x][i];
    if (parent[x] == y) continue;
    parent[y] = x;
    dfs(y);
    d[x] += d[y];
  }
  d[x]++;
}
int check(int x) {
  while (x != 0) {
    int i = x % 10;
    x = x / 10;
    if (i == 4 || i == 7) continue;
    return 0;
  }
  return 1;
}
int dfs1(int x) {
  int i;
  int &result = ldown[x];
  result = 0;
  for (i = (0); i < (a[x].size()); ++i) {
    int j = a[x][i];
    if (parent[x] == j) continue;
    if (b[x][i] == 1) {
      result += d[j];
      dfs1(j);
      continue;
    }
    int value = dfs1(j);
    result += value;
  }
  return result;
}
long long int sum = 0;
void cal(int x1) {
  long long int x = (long long int)x1;
  sum += x * (x - 1);
}
void f(int x) {
  int i;
  for (i = (0); i < (a[x].size()); ++i) {
    int j = a[x][i];
    if (parent[j] != x) continue;
    if (b[x][i] == 1) {
      upper[j] = n - d[j];
      cal(ldown[j] + n - d[j]);
      f(j);
      continue;
    }
    upper[j] = ldown[x] + upper[x] - ldown[j];
    cal(ldown[j] + upper[j]);
    f(j);
  }
}
int main() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  int i;
  for (i = (0); i < (n - 1); ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    a[x].push_back(y);
    a[y].push_back(x);
    z = check(z);
    b[x].push_back(z);
    b[y].push_back(z);
  }
  parent[0] = 0;
  dfs(0);
  dfs1(0);
  upper[0] = 0;
  cal(ldown[0]);
  f(0);
  cout << sum << endl;
  return 0;
}
