#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10, OO = 1e9;
vector<pair<int, int>> g[N];
int n, sub[N], dp[N];
int Luck(int i) {
  while (i) {
    if (i % 10 != 7 && i % 10 != 4) return false;
    i /= 10;
  }
  return true;
}
long long get(long long i) { return i * i - i; }
void dfs(int src, int par) {
  sub[src] = 1;
  for (auto c : g[src]) {
    if (c.first == par) continue;
    dfs(c.first, src);
    sub[src] += sub[c.first];
  }
}
void solvetop(int src, int par) {
  for (auto c : g[src]) {
    if (c.first == par) continue;
    if (c.second == 1) {
      dp[c.first] += (n - sub[c.first]);
    } else {
      dp[c.first] = dp[src];
    }
    solvetop(c.first, src);
  }
}
void solvedown(int src, int par) {
  for (auto c : g[src]) {
    if (c.first == par) continue;
    solvedown(c.first, src);
    if (c.second == 1) {
      dp[src] += sub[c.first];
    } else {
      dp[src] += dp[c.first];
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(b, Luck(c));
    g[b].emplace_back(a, Luck(c));
  }
  dfs(1, -1);
  solvedown(1, -1);
  solvetop(1, -1);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += get(dp[i]);
  }
  cout << ans << endl;
}
