#include <bits/stdc++.h>
using namespace std;
inline int Min(int a, int b) { return a > b ? b : a; }
static inline int rd() {
  unsigned int r = 0;
  char c = getchar();
  char flag = 0;
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') flag = 1, c = getchar();
  for (; c >= '0' && c <= '9'; c = getchar()) r = (r << 3) + (r << 1) + c - '0';
  if (flag) {
    r = ~r, r++;
  }
  return r;
}
int GpC;
int LastEdge[50001], Next[100001], End[100001], ELPtr;
int Result[50001];
bool Visited[100001];
void AddEdge(int start, int end) {
  End[++ELPtr] = end;
  Next[ELPtr] = LastEdge[start];
  LastEdge[start] = ELPtr;
}
int Calc(int thist, vector<int> Dpdcbuf, vector<unsigned char>& Cpidx) {
  int r = GpC - 1;
  for (int i = 0; i < GpC; r++, thist++)
    for (bool onexit = false; !onexit;) {
      onexit = true;
      for (int j = 1; j <= GpC; j++)
        if (Dpdcbuf[j] == 0 && Cpidx[j] % 3 == thist % 3) {
          onexit = false, Dpdcbuf[j] = -1, i++;
          for (int k = LastEdge[j]; k; k = Next[k]) Dpdcbuf[End[k]]--;
        }
    }
  return r;
}
int main() {
  memset(LastEdge, 0, sizeof LastEdge);
  ELPtr = 0;
  GpC = rd();
  vector<int> Dpdc(GpC + 1, 0);
  vector<unsigned char> Cpidx(GpC + 1);
  int i, j, r = 1 << 29, t;
  for (i = 1; i <= GpC; Cpidx[i++] = rd())
    ;
  for (i = 1; i <= GpC; i++) {
    t = rd();
    while (t--) {
      AddEdge(rd(), i);
      Dpdc[i]++;
    }
  }
  for (i = 1; i <= 3; i++) r = Min(r, Calc(i, Dpdc, Cpidx));
  cout << r << endl;
}
