#include <bits/stdc++.h>
using namespace std;
struct MyTree {
  vector<int> disFromRoot, disFromRoot2;
  vector<vector<int> > fnode;
  vector<long long> sumdis;
  vector<long long> nodecount;
  bool initialized, sumdisinitialized;
  int root, size, maxdeep;

 public:
  vector<vector<pair<int, long long> > > edge;
  MyTree(int n) {
    size = n;
    edge.resize(n);
  }
  void addedge(int a, int b, long long dis = 1) {
    edge[a].push_back(make_pair(b, dis));
    edge[b].push_back(make_pair(a, dis));
  }
  void init(int root = 0) {
    maxdeep = 0;
    initialized = true;
    this->root = root;
    disFromRoot2.resize(size, -2);
    disFromRoot.resize(size);
    fnode.push_back(vector<int>(size));
    queue<int> qu;
    qu.push(root);
    vector<int> vd;
    vd.push_back(root);
    fnode[0][root] = root;
    disFromRoot2[root] = 0;
    while (qu.size() > 0) {
      int x = qu.front();
      qu.pop();
      for (int i = 0; i < edge[x].size(); i++) {
        int y = edge[x][i].first;
        if (disFromRoot2[y] != -2) continue;
        disFromRoot2[y] = disFromRoot2[x] + 1;
        disFromRoot[y] = disFromRoot[x] + edge[x][i].second;
        fnode[0][y] = x;
        qu.push(y);
        vd.push_back(y);
        maxdeep = max(maxdeep, disFromRoot2[y]);
      }
    }
    for (int i = 1; (1 << (i - 1)) < maxdeep; i++) {
      fnode.push_back(vector<int>(size));
      for (int j = 0; j < size; j++) {
        fnode[i][j] = fnode[i - 1][fnode[i - 1][j]];
      }
    }
    nodecount.resize(size);
    for (int i = vd.size() - 1; i > 0; i--) {
      nodecount[i]++;
      nodecount[fnode[0][i]] += nodecount[i];
    }
    nodecount[root]++;
    initialized = true;
  }
  int getfnode(int x, int d) {
    if (!initialized) {
      init();
    }
    int res = x;
    int k = 0;
    while (d > 0) {
      if (d & 1) res = fnode[k][res];
      d /= 2;
      k++;
    }
    return res;
  }
  int getsamefnode(int a, int b) {
    if (!initialized) {
      init();
    }
    int ta = disFromRoot2[a], tb = disFromRoot2[b];
    if (ta < tb)
      b = getfnode(b, tb - ta);
    else
      a = getfnode(a, ta - tb);
    if (a == b) return a;
    for (int i = fnode.size() - 1; i >= 0; i--) {
      if (fnode[i][a] != fnode[i][b]) {
        a = fnode[i][a];
        b = fnode[i][b];
      }
    }
    return fnode[0][a];
  }
  long long getdis(int a, int b) {
    int x = getsamefnode(a, b);
    return disFromRoot[a] + disFromRoot[b] - 2 * disFromRoot[x];
  }
  void sumdisinit() {
    if (!initialized) {
      init();
    }
    sumdis.resize(size);
    for (int i = 0; i < size; i++) {
      sumdis[root] += disFromRoot[i];
    }
    queue<int> qu;
    qu.push(root);
    while (qu.size() > 0) {
      int x = qu.front();
      qu.pop();
      for (int i = 0; i < edge[x].size(); i++) {
        int y = edge[x][i].first;
        if (sumdis[y] != 0) continue;
        sumdis[y] = sumdis[x] + (size - 2 * nodecount[y]) * edge[x][i].second;
        qu.push(y);
      }
    }
  }
  long long getsumdis(int a) {
    if (!sumdisinitialized) {
      sumdisinit();
      sumdisinitialized = true;
    }
    return sumdis[a];
  }
};
bool ok(int t) {
  bool okok = true;
  while (t > 0) {
    int r = t % 10;
    if (r != 4 && r != 7) okok = false;
    t /= 10;
  }
  return okok;
}
int main() {
  int n;
  cin >> n;
  MyTree t(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c = ok(c);
    t.addedge(a, b, c);
  }
  t.init();
  vector<long long> c;
  vector<bool> b(n);
  for (int i = 0; i < n; i++) {
    if (b[i]) continue;
    b[i] = true;
    queue<int> q;
    q.push(i);
    int ct = 1;
    while (q.size() > 0) {
      int x = q.front();
      q.pop();
      for (int i = 0; i < t.edge[x].size(); i++) {
        if (b[t.edge[x][i].first]) continue;
        if (t.edge[x][i].second == 0) {
          q.push(t.edge[x][i].first);
          b[t.edge[x][i].first] = true;
          ct++;
        }
      }
    }
    c.push_back(ct);
  }
  long long res = 0;
  for (int i = 0; i < c.size(); i++) {
    res += c[i] * (n - c[i]) * (n - c[i] - 1);
  }
  cout << res << endl;
}
