#include <bits/stdc++.h>
using namespace std;
int c[205], n, gr[205], aux[205], v[205][205];
long long rez = 1e9, ans;
int main() {
  int i, j, k, val, init, ind, nr;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> c[i];
  for (i = 1; i <= n; i++) {
    cin >> k;
    for (j = 1; j <= k; j++) {
      cin >> val;
      v[val][i] = 1;
      aux[i]++;
    }
  }
  for (init = 1; init <= 3; init++) {
    ind = init;
    for (i = 1; i <= n; i++) gr[i] = aux[i];
    nr = n;
    ans = 0;
    while (nr) {
      for (k = 1; k <= n; k++) {
        for (i = 1; i <= n; i++)
          if ((!gr[i]) && (c[i] == ind)) {
            gr[i]--;
            nr--;
            for (j = 1; j <= n; j++)
              if (v[i][j]) gr[j]--;
          }
      }
      ind++;
      if (ind > 3) ind = 1;
      if (nr) ans++;
    }
    rez = min(rez, ans);
  }
  rez += n;
  cout << rez;
  return 0;
}
