#include <bits/stdc++.h>
using namespace std;
int g[210][210], n, rd[210], clr[210];
bool use[210];
int calc(int x) {
  memset(rd, 0, sizeof(rd));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (g[i][j]) rd[j]++;
  memset(use, 0, sizeof(use));
  int sum = 0;
  for (int t = 1; t <= n; ++t) {
    int mt = -1, mx = 5;
    for (int i = 1; i <= n; ++i)
      if (!use[i] && !rd[i] && (((clr[i] + 3 - x) % 3) < mx)) {
        mx = ((clr[i] + 3 - x) % 3);
        mt = i;
      }
    if (mt == -1) {
      return n * 100;
    }
    use[mt] = 1;
    sum += mx + 1;
    x = clr[mt];
    for (int i = 1; i <= n; ++i)
      if (g[mt][i] && !use[i]) {
        rd[i]--;
      }
  }
  return sum;
}
int main() {
  while (scanf("%d", &n) == 1) {
    memset(g, 0, sizeof(g));
    memset(rd, 0, sizeof(rd));
    memset(clr, 0, sizeof(clr));
    for (int i = 1; i <= n; ++i) scanf("%d", &clr[i]);
    for (int i = 1; i <= n; ++i) {
      static int ki;
      scanf("%d", &ki);
      while (ki--) {
        static int x;
        scanf("%d", &x);
        g[x][i] = 1;
      }
    }
    printf("%d\n", min(calc(1), min(calc(2), calc(3))));
  }
  return 0;
}
