#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 999;
bool g[200][200], t;
int deg[200], de[200];
int f[200], p;
void F(int i) {
  int res = 0;
  while (p > 0) {
    t = 0;
    while (t == 0) {
      t = 1;
      for (int j = 0; j < n; j++) {
        if (f[j] == i && de[j] == 0) {
          p--;
          t = 0;
          de[j] = -1;
          for (int k = 0; k < n; k++)
            if (g[j][k] == 1) de[k]--;
        }
      }
    }
    res++;
    i = i % 3 + 1;
  }
  ans = min(ans, res);
  return;
}
int main(void) {
  memset(g, 0, sizeof(g));
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &f[i]);
  for (int i = 0; i < n; i++) {
    scanf("%d", &m);
    deg[i] = m;
    for (int j = 0; j < m; j++) {
      int q;
      scanf("%d", &q);
      g[q - 1][i] = 1;
    }
  }
  for (int i = 1; i <= 3; i++) {
    p = n;
    for (int j = 0; j < n; j++) de[j] = deg[j];
    F(i);
  }
  printf("%d\n", ans + n - 1);
  return 0;
}
