#include <bits/stdc++.h>
using namespace std;
const int MAXN = 210;
int col[MAXN], ind[MAXN];
vector<int> E[MAXN];
bool finish[MAXN];
int cnt, n;
int tind[MAXN], now;
int nex(int cur) { return (cur == 3) ? 1 : cur + 1; }
void DFS(int u) {
  finish[u] = 1;
  for (int i = 0; i < E[u].size(); i++) {
    int v = E[u][i];
    tind[v]--;
    if ((tind[v] == 0) && (!finish[v]) && col[v] == now) DFS(v);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &col[i]);
  for (int i = 1; i <= n; i++) {
    int k, pre;
    scanf("%d", &k);
    ind[i] = k;
    for (int j = 0; j < k; j++) {
      scanf("%d", &pre);
      E[pre].push_back(i);
    }
  }
  memset(finish, false, sizeof(finish));
  cnt = 0x3f3f3f3f;
  for (int st = 1; st <= 3; st++) {
    bool done = false;
    for (int i = 1; i <= n; i++) finish[i] = false;
    for (int i = 1; i <= n; i++) tind[i] = ind[i];
    now = st;
    int times = 0;
    while (!done) {
      done = 1;
      for (int i = 1; i <= n; i++)
        if (!finish[i]) {
          done = 0;
          break;
        }
      if (done) break;
      for (int i = 1; i <= n; i++)
        if ((!finish[i]) && (col[i] == now) && (tind[i] == 0)) DFS(i);
      now = nex(now);
      times++;
    }
    cnt = min(cnt, times);
  }
  printf("%d\n", n + cnt - 1);
  return 0;
}
