#include <bits/stdc++.h>
struct task {
  task() : pc(-1), indegree(0), outgoing() {}
  int pc;
  int indegree;
  std::vector<int> outgoing;
};
int solve(int pc, std::vector<task> tasks) {
  std::queue<int> todo[3];
  for (__decltype((tasks).begin()) t = (tasks).begin(), t_end = (tasks).end();
       t != t_end; ++t)
    if (t->indegree == 0) todo[t->pc].push(t - tasks.begin());
  int time = 0;
  for (std::size_t solved = 0; solved < tasks.size(); pc = (pc + 1) % 3) {
    while (!todo[pc].empty()) {
      int t = todo[pc].front();
      for (__decltype((tasks[t].outgoing).begin())
               n = (tasks[t].outgoing).begin(),
               n_end = (tasks[t].outgoing).end();
           n != n_end; ++n) {
        if (--tasks[*n].indegree == 0) todo[tasks[*n].pc].push(*n);
      }
      todo[pc].pop();
      ++time;
      ++solved;
    }
    ++time;
  }
  return time - 1;
}
int main() {
  int n;
  scanf("%d", &n);
  std::vector<task> tasks(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &tasks[i].pc);
    --tasks[i].pc;
  }
  for (int i = 0; i < n; ++i) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int a;
      scanf("%d", &a);
      --a;
      tasks[a].outgoing.push_back(i);
      ++tasks[i].indegree;
    }
  }
  printf("%d\n",
         std::min(std::min(solve(0, tasks), solve(1, tasks)), solve(2, tasks)));
}
