#include <bits/stdc++.h>
using namespace std;
const int N = 222;
const long long INF = 1e9 + 19;
int n;
int c[N];
vector<int> e[N];
void read() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &c[i]);
    c[i]--;
  }
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int v;
      scanf("%d", &v);
      v--;
      e[i].push_back(v);
    }
  }
}
void solve() {
  int answer = INF;
  for (int st = 0; st < 3; st++) {
    int cntDone = 0;
    int res = 0;
    vector<int> done(n);
    int curD = st;
    for (;;) {
      for (;;) {
        bool flag = 0;
        for (int i = 0; i < n; i++) {
          if (c[i] == curD && done[i] == 0) {
            bool ff = 1;
            for (auto v : e[i]) ff &= done[v];
            if (ff) {
              done[i] = 1;
              flag = 1;
              cntDone++;
            }
          }
        }
        if (!flag) break;
      }
      curD = (curD + 1) % 3;
      if (cntDone == n) break;
      res++;
    }
    res += n;
    answer = min(res, answer);
  }
  cout << answer << endl;
}
void stress() {}
int main() {
  if (1) {
    read();
    solve();
  } else {
    stress();
  }
  return 0;
}
