#include <bits/stdc++.h>
struct nd {
  int done;
  int in;
  int outnode[202];
} node[202], newnode[202];
int place[202], compu[4][202], newcompu[4][202];
void init(int n) {
  for (int i = 1; i <= n; i++) {
    node[i].done = 0;
    node[i].in = 0;
    memset(node[i].outnode + 1, 0, sizeof(int) * n);
  }
}
int search(int placenow, int n) {
  for (int i = 1; i <= n; i++) {
    int temp = newcompu[placenow][i];
    if (temp && !newnode[i].done && !newnode[i].in) {
      newcompu[placenow][i] = 0;
      return i;
    }
  }
  return 0;
}
void renew(int n) {
  for (int i = 1; i <= n; i++) {
    newnode[i] = node[i];
    for (int j = 1; j <= 3; j++) newcompu[j][i] = compu[j][i];
  }
}
int main() {
  int n;
  scanf("%d", &n);
  init(n);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    place[i] = temp;
    compu[temp][i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int temp;
      scanf("%d", &temp);
      node[i].in++;
      node[temp].outnode[i] = 1;
    }
  }
  int ans = 1000000000;
  for (int begin = 1; begin <= n; begin++) {
    renew(n);
    int placenow = place[begin], donething = 0, hour = 0;
    while (1) {
      int todo;
      while (todo = search(placenow, n)) {
        newnode[todo].done = 1;
        donething++;
        hour++;
        int temp;
        for (int i = 1; i <= n; i++) {
          if (temp = newnode[todo].outnode[i]) {
            newnode[i].in--;
          }
        }
      }
      if (donething != n) {
        placenow = placenow % 3 + 1;
        hour++;
      } else
        break;
    }
    if (ans > hour) ans = hour;
  }
  printf("%d\n", ans);
  return 0;
}
