#include <bits/stdc++.h>
using namespace std;
const int maxN = 100100;
struct tl {
  int u, sta;
};
stack<tl> st;
vector<int> ke[maxN], doi[maxN], gt[maxN];
int f[maxN], g[maxN];
int d[maxN];
int n;
int kt(int x) {
  if (x == 0) return 0;
  while (x > 0) {
    if (x % 10 != 7 && x % 10 != 4) return 0;
    x /= 10;
  }
  return 1;
}
void init() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v, x;
    scanf("%i%i%i", &u, &v, &x);
    x = kt(x);
    ke[u].push_back(v);
    ke[v].push_back(u);
    gt[u].push_back(x);
    gt[v].push_back(x);
    doi[v].push_back(ke[u].size() - 1);
    doi[u].push_back(ke[v].size() - 1);
  }
}
tl gan(int u, int sta) {
  tl tam;
  tam.u = u;
  tam.sta = sta;
  return tam;
}
void dfs() {
  st.push(gan(1, 0));
  while (!st.empty()) {
    int u = st.top().u, sta = st.top().sta;
    st.pop();
    bool stop = true;
    for (int i = sta; i < ke[u].size(); i++)
      if (ke[u][i] != 0) {
        ke[ke[u][i]][doi[u][i]] = 0;
        st.push(gan(u, i + 1));
        st.push(gan(ke[u][i], 0));
        stop = false;
        break;
      }
    if (stop) {
      for (int i = 0; i < ke[u].size(); i++)
        if (ke[u][i] > 0) {
          if (gt[u][i] == 1)
            f[u] += d[ke[u][i]];
          else
            f[u] += f[ke[u][i]];
          d[u] += d[ke[u][i]];
        }
      d[u]++;
    }
  }
}
void dfs2() {
  st.push(gan(1, 0));
  while (!st.empty()) {
    int u = st.top().u, sta = st.top().sta;
    st.pop();
    bool stop = true;
    for (int i = sta; i < ke[u].size(); i++)
      if (ke[u][i] > 0) {
        g[ke[u][i]] = g[u] + f[u] - f[ke[u][i]];
        if (gt[u][i] == 1) g[ke[u][i]] = (n - d[ke[u][i]]);
        st.push(gan(u, i + 1));
        st.push(gan(ke[u][i], 0));
        stop = false;
        break;
      }
  }
}
void xuly() {
  dfs();
  dfs2();
  long long ans = 0;
  for (int i = 1; i < n + 1; i++)
    ans += 1ll * f[i] * (f[i] - 1) + 1ll * g[i] * (g[i] - 1) +
           1ll * 2 * f[i] * g[i];
  cout << ans;
}
int main() {
  init();
  xuly();
  fclose(stdin);
  fclose(stdout);
  return 0;
}
