#include <bits/stdc++.h>
using namespace std;
int isL(int first) {
  while (first) {
    if (first % 10 != 4 && first % 10 != 7) return 0;
    first /= 10;
  }
  return 1;
}
int n;
vector<int> edges[100010];
vector<int> ch[100010];
vector<int> le[100010], lc[100010];
int numv[100010], dans[100010], ans[100010];
int was[100010];
void dfsroot(int v) {
  fprintf(stderr, "%d\n", v);
  was[v] = 1;
  numv[v] = 1;
  dans[v] = 0;
  for (int i = 0; i < ((int)((edges[v]).size())); i++) {
    int u = edges[v][i];
    if (was[u]) continue;
    ch[v].push_back(u);
    lc[v].push_back(le[v][i]);
    dfsroot(u);
    numv[v] += numv[u];
    if (le[v][i]) {
      dans[v] += numv[u];
    } else {
      dans[v] += dans[u];
    }
  }
  edges[v].clear();
  le[v].clear();
}
void dfs(int v, int parn, int parnv) {
  int sum = dans[v] + parn;
  ans[v] = sum;
  for (int i = 0; i < ((int)((ch[v]).size())); i++) {
    int u = ch[v][i];
    dfs(u, lc[v][i] ? (numv[v] - numv[u] + parnv) : sum - dans[u],
        numv[v] - numv[u] + parnv);
  }
}
int main() {
  if (!1) {
    freopen(
        ""
        ".in",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, w;
    scanf("%d %d %d", &a, &b, &w), a--, b--;
    edges[a].push_back(b);
    edges[b].push_back(a);
    le[a].push_back(isL(w));
    le[b].push_back(isL(w));
  }
  dfsroot(0);
  dfs(0, 0, 0);
  for (int i = 0; i < n; i++) {
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    res += (long long)ans[i] * (ans[i] - 1);
  }
  printf("%I64d\n", res);
  return 0;
}
