#include <iostream>
#include <vector>

using namespace std;
using tp = long long int;

vector<bool> dep;
vector<vector<tp>> e;

void gd(tp x, tp f, bool _dep = 0) {
  dep[x] = _dep;
  for (auto&& i : e[x]) {
    if (i != f) {
      gd(i, x, !_dep);
    }
  }
}

signed main() {
  tp n;
  scanf("%lld", &n);
  vector<tp> c(2), s(2);
  vector<tp> v(n + 1);
  dep = vector<bool>(n + 1, 0);
  e = vector<vector<tp>>(n + 1);
  for (tp i = 1; i <= n; ++i) {
    scanf("%lld", &v[i]);
  }
  for (tp i = 1; i < n; ++i) {
    tp u, v;
    scanf("%lld%lld", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  gd(1, -1145141919810);
  for (tp i = 1; i <= n; ++i) {
    ++c[dep[i]];
    s[dep[i]] = (s[dep[i]] + v[i]) % 12;
  }
  if (s[0] == s[1]) {
    printf("%lld", c[0] + c[1]);
  } else if ((s[0] + 1) % 12 == s[1]) {
    printf("%lld", c[1]);
  } else if (s[0] == (s[1] + 1) % 12) {
    printf("%lld", c[0]);
  } else {
    puts("0");
  }
  return EXIT_SUCCESS;
}

/*#################################################################
#.................................................................#
#............................This.Code.Was.Created.By.RBTree......#
#.............#......#...............Limiting-Factor..............#
#............#.#....#.#.................Soul-Code.................#
#.............########............................................#
#............#........#..##############################...........#
#...........#..V....V......#..#........................#..#...#...#
#............#........#....#..........###..###..........#..#.#.#..#
#............#..X##X..#..#............#....#.#...........#..#...#.#
#...........#...N##N...#..#...........###..###..........#.........#
#.......MOE..#..@.....#....#.#.#.#...................#.#..........#
#.............########.....#.#.#.##############.#.#..#.#..........#
#..........................#.#.#.#.............#.#.#.#.#..........#
#......#########...........#.#.#.#.................#.#.#..........#
#.....#.........#..........#.#.#.#.................#.#.#..........#
#.#.#.#G#R#A#S#S#.#.#......#.#.#.#.................#.#.#..........#
#.###################......#.#.#.#.................#.#.#..........#
#...........................#.#.#...................#.#...........#
#.................................................................#
#################################################################*/