#include <bits/stdc++.h>
using namespace std;
const double pi = 2.0 * acos(0.0);
const double eps = 1.0e-9;
const int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
const int dr6[] = {1, 1, 0, -1, -1, 0}, dc6[] = {0, 1, 1, 0, -1, -1};
const int dr8[] = {-1, -1, 0, 1, 1, 1, 0, -1},
          dc8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, a[256], k[256];
bool c[256][256];
int go(int z) {
  int que[3][256], hd[3] = {0}, tl[3] = {0}, t[256] = {0}, r = 0;
  bool v[256] = {false}, u[256][256] = {{false}};
  for (int i = 0; i < n; ++i)
    if (k[i] < 1) que[a[i]][tl[a[i]]++] = i, v[i] = true;
  while (hd[0] < tl[0] || hd[1] < tl[1] || hd[2] < tl[2]) {
    while (hd[z] < tl[z]) {
      int x = que[z][hd[z]++];
      ++r;
      for (int i = 0; i < n; ++i)
        if (c[i][x] && !u[i][x]) {
          u[i][x] = true;
          if (++t[i] >= k[i]) que[a[i]][tl[a[i]]++] = i, v[i] = true;
        }
    }
    ++r;
    (++z) %= 3;
  }
  for (int i = 0; i < n; ++i)
    if (!v[i]) return 1000000000;
  return (r - 1);
}
int main(void) {
  int kase = 1000000000;
  for (int ks = 1; ks <= kase && 1 == scanf("%d", &n); ++ks) {
    for (int i = 0; i < n; ++i) scanf("%d", &a[i]), --a[i];
    memset(c, false, sizeof(c));
    for (int i = 0; i < n && 1 == scanf("%d", &k[i]); ++i)
      for (int j = k[i], x; j-- > 0 && 1 == scanf("%d", &x);)
        c[i][x - 1] = true;
    printf("%d\n", min(go(0), min(go(1), go(2))));
  }
  return 0;
}
