#include <bits/stdc++.h>
using namespace std;
const int MAXN = 256;
int n, A[MAXN], indeg[MAXN];
vector<int> G[MAXN];
bool r[MAXN];
int sim(int id) {
  memset(r, false, sizeof r);
  memset(indeg, 0, sizeof indeg);
  for (int i = (0); i < (int)(n); i++)
    for (int j = (0); j < (int)(G[i].size()); j++) indeg[G[i][j]]++;
  int ans = 0, k = 0;
  while (k < n) {
    bool ok = true;
    while (ok) {
      ok = false;
      for (int i = (0); i < (int)(n); i++) {
        if (!r[i] && indeg[i] == 0 && A[i] == id) {
          r[i] = true;
          for (int j = (0); j < (int)(G[i].size()); j++) indeg[G[i][j]]--;
          ok = true;
          k++;
        }
      }
    }
    if (k == n) break;
    ans++;
    id = (id + 1) % 3;
  }
  return ans;
}
int main() {
  while (scanf("%d", &n) >= 1) {
    for (int i = (0); i < (int)(n); i++) {
      scanf("%d", &A[i]);
      --A[i];
    }
    for (int i = (0); i < (int)(n); i++) {
      int k, a;
      scanf("%d", &k);
      for (int j = (0); j < (int)(k); j++) {
        scanf("%d", &a);
        G[--a].push_back(i);
      }
    }
    printf("%d\n", min(min(sim(0), sim(1)), sim(2)) + n);
  }
  return 0;
}
