#include <bits/stdc++.h>
using namespace std;
vector<int> dep[500];
vector<int> com[3];
int deg[500];
int nc[3];
int n;
int sol(int st) {
  int i, j, k;
  int ch = 0;
  int mv = 0;
  int rem = n;
  int sol[500];
  int red[500];
  vector<int>::iterator it, iu;
  for (i = 0; i < n; i++) {
    sol[i] = 0;
    red[i] = deg[i];
  }
  for (; rem > 0; mv++, st++, st %= 3) {
    for (ch = 1; ch;) {
      ch = 0;
      for (it = com[st].begin(); it != com[st].end(); it++) {
        if (sol[*it] == 0 and red[*it] == 0) {
          sol[*it] = 1;
          rem--;
          ch = 1;
          for (iu = dep[*it].begin(); iu != dep[*it].end(); iu++) red[*iu]--;
        }
      }
    }
  }
  return mv - 1 + n;
}
int main() {
  int i, j, k;
  scanf("%d", &n);
  nc[0] = nc[1] = nc[2] = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &k);
    k--;
    com[k].push_back(i);
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &deg[i]);
    for (k = 0; k < deg[i]; k++) {
      scanf("%d", &j);
      j--;
      dep[j].push_back(i);
    }
  }
  printf("%d\n", min(sol(0), min(sol(1), sol(2))));
  return 0;
}
