#include <bits/stdc++.h>
using namespace std;
int N, C[200];
bool P[200][200];
bool Q[200][200];
int id[200];
bool done[200];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C[i];
    C[i]--;
  }
  int K;
  for (int i = 0; i < N; i++) {
    cin >> K;
    int a;
    for (int j = 0; j < K; j++) {
      cin >> a;
      P[i][a - 1] = 1;
    }
  }
  int best = 1000000;
  for (int q = 0; q < 3; q++) {
    int at = q, cost = 0;
    for (int i = 0; i < N; i++) id[i] = 0, done[i] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        Q[j][i] = P[i][j];
        if (Q[j][i]) id[i]++;
      }
    }
    int left = N;
    while (left > 0) {
      bool anyhere = 0;
      for (int i = 0; i < N; i++) {
        if (!done[i] && id[i] == 0 && C[i] == at) {
          anyhere = 1;
          for (int j = 0; j < N; j++)
            if (Q[i][j]) id[j]--;
          left--;
          cost++;
          done[i] = 1;
        }
      }
      if (!anyhere) {
        cost++;
        at = (at + 1) % 3;
      }
    }
    best = min(best, cost);
  }
  cout << best << endl;
  return 0;
}
