#include <bits/stdc++.h>
using namespace std;
int a[202];
int w[202][202];
int qe[202];
int qw[202];
int n;
int judge(int x) {
  int sum = 0;
  int gao = 0;
  while (1) {
    if (gao == n) return sum;
    int i;
    for (i = 1; i <= n; i++) {
      if (qw[i] == 0 && a[i] == x) break;
    }
    if (i == n + 1) {
      sum++;
      x = (x + 1) % 3;
      continue;
    }
    gao++;
    sum++;
    int j;
    qw[i] = -1;
    for (j = 1; j <= n; j++) {
      if (w[i][j] == 1) qw[j]--;
    }
  }
}
int main() {
  scanf("%d", &n);
  int i;
  memset(w, 0, sizeof(w));
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  for (i = 1; i <= n; i++) {
    int r;
    scanf("%d", &r);
    int j;
    for (j = 1; j <= r; j++) {
      int x;
      scanf("%d", &x);
      w[x][i] = 1;
    }
    qe[i] = r;
  }
  int cao = 10000000;
  for (i = 0; i <= 2; i++) {
    int j;
    for (j = 1; j <= n; j++) qw[j] = qe[j];
    cao = min(cao, judge(i));
  }
  printf("%d\n", cao);
  return 0;
}
