#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
;
vector<pair<int, int> > adj[N];
vector<int> lis;
set<int> lucky_ver;
int sum, dem[N], ar[10], mul[10];
;
bool used[N];
void dfs(int x) {
  for (int i = 0; i < adj[x].size(); i++) {
    if (!lucky_ver.count(adj[x][i].second) && !used[adj[x][i].first]) {
      sum++;
      used[adj[x][i].first] = true;
      lis.push_back(adj[x][i].first);
      dfs(adj[x][i].first);
    }
  }
}
void check(int x) {
  int sum = 0;
  for (int i = 0; i < x; i++) {
    if (ar[i] == 1) {
      sum += 4 * mul[i];
    } else {
      sum += 7 * mul[i];
    }
  }
  lucky_ver.insert(sum);
}
void back(int pos, int x) {
  if (pos == x) {
    check(x);
    return;
  }
  ar[pos] = 1;
  back(pos + 1, x);
  ar[pos] = 0;
  back(pos + 1, x);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, i, j, k, l;
  long long ans = 0;
  set<int>::iterator ite;
  cin >> n;
  for (i = 1; i < n; i++) {
    cin >> j >> k >> l;
    adj[j].push_back({k, l});
    adj[k].push_back({j, l});
  }
  mul[0] = 1;
  for (i = 1; i <= 9; i++) {
    mul[i] = mul[i - 1] * 10;
    back(0, i);
  }
  for (i = 1; i <= n; i++) {
    if (!used[i]) {
      used[i] = true;
      lis.push_back(i);
      sum = 1;
      dfs(i);
      for (j = 0; j < lis.size(); j++) {
        dem[lis[j]] = sum;
      }
      lis.clear();
    }
  }
  for (i = 1; i <= n; i++) {
    ans += 1ll * (n - dem[i]) * (n - dem[i] - 1);
  }
  cout << ans;
}
