#include <bits/stdc++.h>
using namespace std;
int n, c[210], p[210], d[210];
bool b[210], a[210][210];
int find(int k) {
  int i;
  for (i = 1; i <= n; i++) {
    if (c[i] == k) {
      if (d[i] == 0 && b[i]) {
        return i;
      }
    }
  }
  return n + 1;
}
int calc(int k) {
  memset(b, true, sizeof(b));
  for (int i = 1; i <= n; i++) d[i] = p[i];
  int ret = 0;
  for (int z = 1; z <= n; z++) {
    int x = find(k);
    while (x > n) {
      k++;
      if (k == 4) k = 1;
      ret++;
      x = find(k);
    }
    b[x] = false;
    ret++;
    for (int i = 1; i <= n; i++)
      if (a[i][x]) d[i]--;
  }
  return ret;
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &c[i]);
  memset(a, false, sizeof(a));
  for (i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    for (j = 1; j <= p[i]; j++) {
      int y;
      scanf("%d", &y);
      a[i][y] = true;
    }
  }
  int ans = calc(1);
  int temp;
  temp = calc(2);
  if (temp < ans) ans = temp;
  temp = calc(3);
  if (temp < ans) ans = temp;
  printf("%d\n", ans);
  return 0;
}
