#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void inc(T &U) {
  ++U;
}
template <class T>
inline void dec(T &U) {
  --U;
}
const long limit = 100000 + 5;
const long maxn = 1000000000;
void lam();
void ktao();
void inkq();
void doctest(long &i);
long m, n;
long kq = maxn;
bool b[limit];
long d[limit];
long e[limit];
list<long> a[limit];
long c[limit];
long h[limit];
int main() {
  long i, j, k, t;
  doctest(k);
  for (i = (1); i <= (k); ++i) {
    ktao();
    lam();
    inkq();
  }
}
void doctest(long &i) { i = 1; }
void inkq() {
  long i, j, k, t, integer;
  printf("%ld", kq + n);
}
void ktao() {
  long i, j, k, t, integer;
  cin >> n;
  for (i = (1); i <= (n); ++i) {
    scanf("%ld", &c[i]);
  }
  for (i = (1); i <= (n); ++i) {
    scanf("%ld", &e[i]);
    for (j = (1); j <= (e[i]); ++j) {
      scanf("%ld", &k);
      a[k].push_back(i);
    }
  }
}
void lam() {
  long i, j, k, t, dau, cuoi, integer;
  multimap<long, long> q;
  multimap<long, long>::iterator p, pp;
  list<long>::iterator z;
  for (k = (1); k <= (3); ++k) {
    q.clear();
    for (i = (1); i <= (n); ++i) {
      b[i] = true;
      d[i] = e[i];
      q.insert(pair<long, long>(d[i], i));
    }
    j = k;
    t = 0;
    long nn = n;
    while (nn > 0) {
      dau = 1;
      cuoi = 0;
      for (i = (1); i <= (n); ++i)
        if (b[i] && d[i] == 0 && c[i] == j) h[++cuoi] = i;
      while (dau <= cuoi) {
        i = h[dau++];
        nn--;
        b[i] = false;
        for (z = (a[i].begin()); z != (a[i].end()); z++) {
          d[*z]--;
          if (d[*z] == 0 && c[*z] == j) h[++cuoi] = (*z);
        }
      }
      if (nn > 0) {
        t++;
        j++;
        if (j > 3) j = 1;
      }
    }
    kq = min(kq, t);
  }
}
