#include <bits/stdc++.h>
using namespace std;
const int maxN = 100010;
int n, parent[maxN], child[maxN];
long long f[maxN], g[maxN], res;
struct ioi {
  int v, w;
};
vector<ioi> adj[maxN];
void dfs(int u) {
  child[u] = 1;
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i].v;
    if (parent[u] == v) continue;
    parent[v] = u;
    dfs(v);
    child[u] = child[u] + child[v];
    f[u] += ((adj[u][i].w) ? child[v] : f[v]);
  }
}
void dfs_g(int u) {
  for (int i = 0; i < adj[u].size(); ++i) {
    int v = adj[u][i].v;
    if (parent[u] == v) continue;
    int tmp = ((adj[u][i].w) ? child[v] : f[v]);
    g[v] = (adj[u][i].w) ? (n - child[v]) : (f[u] + g[u] - tmp);
    dfs_g(v);
  }
}
bool check(int x) {
  while (x) {
    if (((x % 10) != 4) && ((x % 10) != 7)) return false;
    x /= 10;
  }
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    w = check(w);
    ioi tmp;
    tmp.v = v;
    tmp.w = w;
    adj[u].push_back(tmp);
    tmp.v = u;
    tmp.w = w;
    adj[v].push_back(tmp);
  }
  dfs(1);
  dfs_g(1);
  for (int i = 1; i <= n; ++i) res += ((f[i] + g[i]) * (f[i] + g[i] - 1));
  cout << res;
  fclose(stdin);
  fclose(stdout);
}
