#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int> > c(3);
  vector<int> countPred(n);
  for (int i = 0; i < n; i++) {
    int buf;
    cin >> buf;
    c[buf - 1].push_back(i);
  }
  vector<vector<int> > graph(n);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      graph[v - 1].push_back(i);
      countPred[i]++;
    }
  }
  vector<int> cp = countPred;
  int minAns = 2000000;
  for (int t = 0; t < 3; t++) {
    countPred = cp;
    int countGame = n;
    int i = t;
    int ans = 0;
    bool start = false;
    while (countGame) {
      vector<int> nullGame;
      for (vector<int>::iterator j = c[i].begin(); j != c[i].end(); j++) {
        if (countPred[*j] == 0) {
          start = true;
          nullGame.push_back(*j);
          countPred[*j] = -1;
        }
      }
      if (nullGame.size()) {
        for (int k = 0; k < nullGame.size(); k++) {
          int currGame = nullGame[k];
          for (vector<int>::iterator j = graph[currGame].begin();
               j != graph[currGame].end(); j++) {
            countPred[*j]--;
          }
          for (vector<int>::iterator j = c[i].begin(); j != c[i].end(); j++) {
            if (countPred[*j] == 0) {
              start = true;
              nullGame.push_back(*j);
              countPred[*j] = -1;
            }
          }
          countGame--;
          ans++;
        }
      }
      if (countGame && start) ans++;
      i++;
      if (i == 3) i = 0;
    }
    minAns = min(ans, minAns);
  }
  cout << minAns;
  return 0;
}
