#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int inf = (int)1e9;
const int maxn = 210;
const int maxe = maxn * maxn * 2;
int a[maxn];
int head[maxn];
int next_asdf[maxe];
int to[maxe];
int mark[maxn];
int n;
int leftt = 0;
int p[maxn], pc = 0;
int vc = 1;
void add(int x, int y) {
  vc++;
  to[vc] = y;
  next_asdf[vc] = head[x];
  head[x] = vc;
}
bool can(int x) {
  for (int e = head[x]; e; e = next_asdf[e])
    if (mark[to[e]] == 0) return false;
  return true;
}
int go(int x) {
  for (int i = 0, j = p[0]; i < n; i++, j = p[i])
    if (a[j] == x && mark[j] == 0 && can(j)) {
      mark[j] = 1;
      leftt--;
    }
  if (leftt == 0)
    return 0;
  else
    return go((x + 1) % 3) + 1;
}
int Do(int x) {
  memset(mark, 0, sizeof mark);
  leftt = n;
  return go(x) + n;
}
void dfs(int x) {
  mark[x] = 1;
  for (int e = head[x]; e; e = next_asdf[e])
    if (mark[to[e]] == 0) dfs(to[e]);
  p[pc++] = x;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) a[i]--;
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int x;
      scanf("%d", &x);
      add(i, x - 1);
    }
  }
  for (int i = 0; i < n; i++)
    if (mark[i] == 0) dfs(i);
  int res = min(Do(0), min(Do(1), Do(2)));
  printf("%d\n", res);
  return 0;
}
