#include <bits/stdc++.h>
int min(int a, int b) { return a < b ? a : b; }
int next[1 + (200 * (200 - 1) / 2)], jj[1 + (200 * (200 - 1) / 2)];
int link(int l, int j) {
  static int l_ = 1;
  next[l_] = l;
  jj[l_] = j;
  return l_++;
}
int ao[200], cc[200], dd[200], qq[3][200];
int solve(int n, int c_) {
  static int dd_[200], head[3], cnt[3];
  int i, j, l, k, t, c;
  memcpy(dd_, dd, n * sizeof *dd);
  memset(head, 0, sizeof head);
  memset(cnt, 0, sizeof cnt);
  for (i = 0; i < n; i++)
    if (dd_[i] == 0) {
      c = cc[i];
      qq[c][head[c] + cnt[c]++] = i;
    }
  k = 0, t = 0;
  while (k < n) {
    while (cnt[c_]) {
      i = qq[c_][cnt[c_]--, head[c_]++];
      k++, t++;
      for (l = ao[i]; l; l = next[l]) {
        j = jj[l];
        if (--dd_[j] == 0) {
          c = cc[j];
          qq[c][head[c] + cnt[c]++] = j;
        }
      }
    }
    c_ = (c_ + 1) % 3;
    t++;
  }
  return t - 1;
}
int main() {
  int n, i, j, k;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &cc[i]), cc[i]--;
  for (i = 0; i < n; i++) {
    scanf("%d", &k);
    while (k--) {
      scanf("%d", &j), j--;
      ao[j] = link(ao[j], i);
      dd[i]++;
    }
  }
  printf("%d\n", min(solve(n, 0), min(solve(n, 1), solve(n, 2))));
  return 0;
}
