#include <bits/stdc++.h>
using namespace std;
int a[210][210], in[210], i, j, k, m, n, x, y, z, c[210], used[210];
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &in[i]);
    for (j = 1; j <= in[i]; j++) scanf("%d", &a[i][j]);
  }
  int ans = 10000;
  for (int st = 1; st <= 3; st++) {
    memset(used, 0, sizeof(used));
    int t = st;
    m = n;
    int q = 0;
    while (m > 0) {
      while (1) {
        int f1 = 0;
        for (i = 1; i <= n; i++)
          if (used[i] == false && c[i] == t) {
            int flag = 1;
            for (j = 1; j <= in[i]; j++)
              if (used[a[i][j]] == 0) {
                flag = false;
                break;
              }
            if (flag) {
              m--;
              f1 = true;
              used[i] = 1;
            }
          }
        if (!f1) break;
      }
      t = t % 3 + 1;
      q++;
    }
    if (q - 1 < ans) ans = q - 1;
  }
  printf("%d\n", ans + n);
}
