#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
template <class T>
inline void read(T& x, T& y) {
  read(x);
  read(y);
}
template <class T>
inline void read(T& x, T& y, T& z) {
  read(x);
  read(y);
  read(z);
}
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
int n, i, j, k, l, p, now, x, c[210];
int e[210][210], D[210], d[210];
bool v[210];
int main() {
  read(n);
  for (i = 1; i <= n; i++) read(c[i]);
  for (i = 1; i <= n; i++) {
    for (read(p); p; p--) {
      read(x);
      e[x][++e[x][0]] = i;
      D[i]++;
    }
  }
  int ans = int(1e9);
  for (x = 1; x <= 3; x++) {
    memcpy(d, D, sizeof(D));
    int res = 0;
    now = x;
    memset(v, 0, sizeof(v));
    for (l = 1; l <= n; l++) {
      p = 0;
      for (i = 1; i <= n; i++)
        if (d[i] == 0 && c[i] == now) p = i;
      if (p) {
        d[p] = -1, res++;
        for (i = 1; i <= e[p][0]; i++) d[e[p][i]]--;
        continue;
      }
      now = now % 3 + 1;
      l--;
      res++;
    }
    ans = min(ans, res);
  }
  printf("%d\n", ans);
  scanf("\n");
  return 0;
}
