#include <bits/stdc++.h>
struct {
  short com;
  short flg[3];
  short num;
  short bef[199];
} game[200] = {{0}};
int chk_clear(int k, int f);
int main(void) {
  int i, j, k, in, n, room;
  int count[3] = {0}, cleard[3] = {0};
  scanf("%d%*c", &n);
  for (i = 0; i < n; i++) scanf("%hd%*c", &game[i].com);
  for (i = 0; i < n; i++) {
    scanf("%hd", &game[i].num);
    for (j = 0; j < game[i].num; j++) {
      scanf("%hd", &game[i].bef[j]);
    }
  }
  for (i = 0; i < 3; i++) {
    room = i;
    do {
      room %= 3;
      room++;
      for (j = 0; j < n; j++) {
        if (game[j].com != room) continue;
        if (game[j].flg[i] == 1) continue;
        in = chk_clear(j, i);
        if (in == 1) {
          game[j].flg[i] = 1;
          cleard[i]++;
          j = -1;
        }
      }
    } while (cleard[i] < n && ++count[i]);
    count[i] += n;
  }
  printf("%d\n",
         (((count[0]) < ((((count[1]) < (count[2])) ? (count[1]) : (count[2]))))
              ? (count[0])
              : ((((count[1]) < (count[2])) ? (count[1]) : (count[2])))));
  return 0;
}
int chk_clear(int k, int f) {
  int i;
  for (i = 0; i < game[k].num; i++) {
    if (game[game[k].bef[i] - 1].flg[f] == 0) return 0;
  }
  return 1;
}
