#include <bits/stdc++.h>
using namespace std;
int rt, i, n, u[1000], keep[1000], a[1000][1000], flag, remain, c[1000], j, ret,
    x, keep2[1000];
void solve(int k) {
  rt = 0;
  for (i = 1; i <= n; i++) u[i] = 0;
  remain = n;
  while (1) {
    flag = 1;
    while (flag) {
      flag = 0;
      for (i = 1; i <= n; i++)
        if (c[i] == k && !keep[i] && !u[i]) {
          u[i] = 1;
          for (j = 1; j <= n; j++) keep[j] -= a[i][j];
          flag = 1;
          --remain;
        }
    }
    ++k;
    ++rt;
    if (!remain) break;
    if (k == 4) k = 1;
  }
  for (i = 1; i <= n; i++) keep[i] = keep2[i];
  ret = min(ret, rt);
}
int main(int argc, char* const argv[]) {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &keep[i]);
    keep2[i] = keep[i];
    for (j = 1; j <= keep[i]; j++) {
      scanf("%d", &x);
      a[x][i] = 1;
    }
  }
  ret = 1000000000;
  solve(1);
  solve(2);
  solve(3);
  printf("%d\n", ret + n - 1);
  return 0;
}
