#include <bits/stdc++.h>
using namespace std;
const int N = 202;
vector<int> sp[10];
int c[N], a[N][N], i, k[N], n, mn, kol, p, j, z, usd[N], tt;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> c[i], --c[i], sp[c[i]].push_back(i);
  for (i = 1; i <= n; ++i) {
    cin >> k[i];
    for (j = 1; j <= k[i]; ++j) cin >> a[i][j];
  }
  mn = 1000000000;
  for (i = 0; i < 3; ++i) {
    kol = n;
    p = i;
    memset(usd, 0, sizeof(usd));
    int ans = 0;
    while (kol) {
      int ttt = 1;
      while (ttt) {
        ttt = 0;
        for (j = 0; j < (int)sp[p].size(); ++j) {
          int v = sp[p][j];
          if (!usd[v]) {
            tt = 0;
            for (z = 1; z <= k[v]; ++z)
              if (!usd[a[v][z]]) {
                tt = 1;
                break;
              }
            if (!tt) usd[v] = 1, --kol, ++ans, ttt = 1;
          }
        }
      }
      if (!kol) break;
      ++p;
      p %= 3;
      ++ans;
      if (ans > mn) break;
    }
    mn = min(mn, ans);
  }
  cout << mn << endl;
  return 0;
}
