#include <bits/stdc++.h>
using namespace std;
int a[202], graph[202][202];
int main() {
  int n;
  cin >> n;
  for (int(i) = (int)(0); (i) < (int)(n); ++(i)) {
    cin >> a[i];
    --a[i];
  }
  memset(graph, 0, sizeof(graph));
  for (int(i) = (int)(0); (i) < (int)(n); ++(i)) {
    int m;
    cin >> m;
    for (int(j) = (int)(0); (j) < (int)(m); ++(j)) {
      int tmp;
      cin >> tmp;
      graph[tmp - 1][i] = 1;
    }
  }
  int ans = 987654321;
  for (int s = 0; s <= 2; ++s) {
    int cost = 0;
    int now = s;
    vector<int> done(n, 0);
    int ndone = 0;
    while (ndone < n) {
      for (int(k) = (int)(0); (k) < (int)(n); ++(k)) {
        for (int(i) = (int)(0); (i) < (int)(n); ++(i))
          if (a[i] == now && done[i] == 0) {
            bool ok = true;
            for (int(j) = (int)(0); (j) < (int)(n); ++(j))
              if (graph[j][i] == 1 && done[j] == 0) {
                ok = false;
                break;
              }
            if (ok) {
              done[i] = 1;
              ++ndone;
              ++cost;
              break;
            }
          }
      }
      now = (now + 1) % 3;
      ++cost;
    }
    ans = min(ans, cost - 1);
  }
  cout << ans << endl;
  return 0;
}
