#include <bits/stdc++.h>
using namespace std;
struct node {
  int to, next, w;
} edge[220 * 220];
int n;
int cnt, pre[220];
int mark[220], sign[220];
int in[220];
int save[220];
int mi;
int sum;
void add_edge(int u, int v, int w) {
  edge[cnt].to = v;
  edge[cnt].w = w;
  edge[cnt].next = pre[u];
  pre[u] = cnt++;
}
void dfs(int s, int k) {
  for (int ii = 0; ii < n; ii++)
    for (int i = 1; i <= n; i++) {
      if (sign[i] == 0 && mark[i] == s && in[i] == 0) {
        k++;
        sign[i] = 1;
        for (int p = pre[i]; p != -1; p = edge[p].next)
          if (sign[edge[p].to] == 0) in[edge[p].to]--;
      }
    }
  if (k == n) return;
  sum++;
  if (s == 3)
    s = 1;
  else
    s++;
  dfs(s, k);
}
int main() {
  cnt = 0;
  memset(pre, -1, sizeof(pre));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &mark[i]);
  }
  for (int i = 1; i <= n; i++) {
    int tmp;
    scanf("%d", &tmp);
    for (int j = 0; j < tmp; j++) {
      int to;
      scanf("%d", &to);
      add_edge(to, i, 1);
      save[i]++;
    }
  }
  mi = 9999999;
  for (int i = 1; i <= 3; i++) {
    sum = 0;
    memset(sign, 0, sizeof(sign));
    for (int j = 1; j <= n; j++) {
      in[j] = save[j];
    }
    dfs(i, 0);
    if (sum < mi) mi = sum;
  }
  printf("%d", mi + n);
  return 0;
}
