#include <bits/stdc++.h>
using namespace std;
int n, c[210], ki[210], a[210][210];
bool used[210];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    cin >> ki[i];
    for (int j = 0; j < ki[i]; j++) {
      cin >> a[i][j];
      a[i][j]--;
    }
  }
  int ans = 1 << 30, comp[3] = {1, 2, 3};
  for (int i = 0; i < 3; i++) {
    int counter = 0, sum = -1, turn = i;
    memset(used, 0, sizeof(used));
    while (counter < n) {
      sum++;
      bool change = true;
      while (change) {
        change = false;
        for (int j = 0; j < n; j++) {
          if (!used[j] && comp[turn % 3] == c[j]) {
            bool flag = true;
            for (int k = 0; k < ki[j]; k++)
              if (!used[a[j][k]]) flag = false;
            if (flag) {
              counter++;
              used[j] = true;
              change = true;
              sum++;
            }
          }
        }
      }
      turn++;
    }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
