#include <bits/stdc++.h>
using namespace std;
template <class _T>
inline _T sqr(const _T& x) {
  return x * x;
}
template <class _T>
inline string tostr(const _T& a) {
  ostringstream os("");
  os << a;
  return os.str();
}
const long double PI = 3.1415926535897932384626433832795L;
const long double EPS = 1e-11;
char TEMPORARY_CHAR;
const int INF = 1e9;
const int N = 1 << 16;
inline void input(int& a) {
  a = 0;
  while ((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') {
  }
  while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0') {
    a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
    TEMPORARY_CHAR = getchar();
  }
}
inline int nxt() {
  int(ret);
  input((ret));
  ;
  return ret;
}
int main() {
  int n = nxt();
  vector<int> type(n);
  for (int i = 0; i < (int)(n); i++) type[i] = nxt() - 1;
  vector<vector<int> > g(n);
  vector<int> cnt(n);
  for (int i = 0; i < (int)(n); i++) {
    cnt[i] = nxt();
    for (int j = 0; j < (int)(cnt[i]); j++) g[nxt() - 1].push_back(i);
  }
  int ans = INF;
  for (int st = 0; st < (int)(3); st++) {
    int cur_ans = 0;
    queue<int> a[3];
    vector<int> c = cnt;
    for (int i = 0; i < (int)(n); i++)
      if (!c[i]) a[type[i]].push(i);
    int cur = st;
    while (!a[0].empty() || !a[1].empty() || !a[2].empty()) {
      while (!a[cur].empty()) {
        cur_ans++;
        int v = a[cur].front();
        a[cur].pop();
        for (__typeof((g[v]).begin()) it = (g[v]).begin(); it != (g[v]).end();
             it++) {
          int to = *it;
          c[to]--;
          if (!c[to]) a[type[to]].push(to);
        }
      }
      if (!a[0].empty() || !a[1].empty() || !a[2].empty()) {
        cur_ans++;
        cur = (cur + 1) % 3;
      }
    }
    ans = min(ans, cur_ans);
  }
  cout << ans;
  return 0;
}
