#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 123;
int n;
vector<pair<int, int> > tree[N];
long long dp[N][3];
long long summ = 0;
bool isluck(int a) {
  while (a) {
    if (((a % 10) != 7) && ((a % 10) != 4)) return false;
    a /= 10;
  }
  return true;
}
void dfs(int p, int f) {
  dp[p][0] = 0;
  dp[p][1] = 0;
  for (int i = 0; i < tree[p].size(); ++i) {
    int t = tree[p][i].first;
    int v = tree[p][i].second;
    if (t != f) {
      dfs(t, p);
      dp[p][0] += dp[t][0] + 1;
      if (v)
        dp[p][1] += dp[t][0] + 1;
      else
        dp[p][1] += dp[t][1];
    }
  }
}
void cals(int p, int f) {
  for (int i = 0; i < tree[p].size(); ++i) {
    int t = tree[p][i].first;
    int v = tree[p][i].second;
    if (t == f) continue;
    if (v) {
      dp[t][2] = n - 1 - dp[t][0] + dp[t][1];
    } else {
      dp[t][2] = dp[p][2];
    }
    cals(t, p);
  }
  summ += dp[p][2] * (dp[p][2] - 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    if (isluck(w))
      w = 1;
    else
      w = 0;
    tree[a].push_back(make_pair(b, w));
    tree[b].push_back(make_pair(a, w));
  }
  dfs(1, 0);
  dp[1][2] = dp[1][1];
  cals(1, 0);
  cout << summ << endl;
  return 0;
}
