#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<vector<int> > comp(3, vector<int>()), dep(n, vector<int>());
  for (int i = 0; i < (int)(n); i++) {
    int a;
    cin >> a;
    comp[--a].push_back(i);
  }
  for (int i = 0; i < (int)(n); i++) {
    int k;
    cin >> k;
    for (int j = 0; j < (int)(k); j++) {
      int a;
      cin >> a;
      dep[i].push_back(--a);
    }
  }
  int mini = 1000000000;
  vector<int> done(n, false);
  for (int st = 0; st < (int)(3); st++) {
    int t = 0, completed = 0;
    fill((done).begin(), (done).end(), false);
    for (int c = st, wt = 0; wt <= 3; c = (c + 1) % 3, t++, wt++) {
      bool imp = true;
      while (imp) {
        imp = false;
        for (typeof((comp[c]).begin()) task = (comp[c]).begin();
             task != (comp[c]).end(); task++) {
          int i = *task;
          if (done[i]) continue;
          bool doable = true;
          for (typeof((dep[i]).begin()) it = (dep[i]).begin();
               it != (dep[i]).end(); it++) {
            doable = doable && done[*it];
          }
          if (doable) {
            done[i] = true;
            t++;
            completed++;
            imp = true;
            wt = 0;
          }
        }
      }
      if (completed == n) break;
    }
    mini = min(mini, t);
  }
  cout << mini << endl;
}
int main() {
  solve();
  return 0;
}
