#include <bits/stdc++.h>
using namespace std;
vector<int> c;
vector<set<int> > g;
int n;
int tryComp(int comp) {
  int res = 0;
  vector<set<int> > gg = g;
  vector<bool> use(n);
  set<int> cur[3];
  for (int i = 0; i < n; ++i) {
    if (gg[i].size() == 0) {
      cur[c[i]].insert(i);
    }
  }
  while (!(cur[0].empty() && cur[1].empty() && cur[2].empty())) {
    if (cur[comp].empty()) {
      ++res;
      comp = (comp + 1) % 3;
      continue;
    }
    while (!cur[comp].empty()) {
      int a = *cur[comp].begin();
      ++res;
      use[a] = true;
      cur[comp].erase(cur[comp].begin());
      for (int i = 0; i < n; ++i) {
        gg[i].erase(a);
        if (gg[i].empty() && !use[i]) {
          cur[c[i]].insert(i);
        }
      }
    }
  }
  return res;
}
int main() {
  cin >> n;
  c.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
  }
  g.resize(n);
  for (int i = 0; i < n; ++i) {
    int m;
    cin >> m;
    int a;
    for (int j = 0; j < m; ++j) {
      cin >> a;
      g[i].insert(--a);
    }
  }
  cout << min(tryComp(0), min(tryComp(1), tryComp(2)));
}
