#include <bits/stdc++.h>
using namespace std;
int fir[201], tot, in[201], In[201];
int nxt[400201], to[400201];
inline void add_edge(int u, int v) {
  to[++tot] = v;
  nxt[tot] = fir[u];
  fir[u] = tot;
}
int n, c[201], cc[4][201];
queue<int> q[5];
inline int solve(int x) {
  int ans = 0;
  for (register int i = 1; i <= 3; i++) {
    for (register int j = 1; j <= cc[i][0]; j++) {
      if (!in[cc[i][j]]) q[i].push(cc[i][j]);
    }
  }
  int now = x - 1;
  while (1) {
    now++;
    if (now == 4) now = 1;
    while (!q[now].empty()) {
      ans++;
      int u = q[now].front();
      q[now].pop();
      for (register int i = fir[u]; i; i = nxt[i]) {
        int v = to[i];
        in[v]--;
        if (!in[v]) {
          q[c[v]].push(v);
        }
      }
    }
    if (q[1].empty() && q[2].empty() && q[3].empty()) return ans;
    ans++;
  }
}
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; i++) {
    scanf("%d", c + i);
    cc[c[i]][++cc[c[i]][0]] = i;
  }
  for (register int i = 1; i <= n; i++) {
    int sze = 0, tmp = 0;
    scanf("%d", &sze);
    for (register int j = 1; j <= sze; j++) {
      scanf("%d", &tmp);
      add_edge(tmp, i);
      In[i]++;
      in[i]++;
    }
  }
  int ans = 0x7fffffff;
  ans = min(ans, solve(1));
  for (register int i = 1; i <= n; i++) in[i] = In[i];
  ans = min(ans, solve(2));
  for (register int i = 1; i <= n; i++) in[i] = In[i];
  ans = min(ans, solve(3));
  printf("%d\n", ans);
  return 0;
}
