#include <bits/stdc++.h>
using namespace std;
const int MAX = 205;
int c[MAX];
bool adj[MAX][MAX];
int deg[MAX];
int tmp[MAX];
int N;
bool mark[MAX];
int solve(int s) {
  memset(mark, 0, sizeof mark);
  memcpy(tmp, deg, sizeof deg);
  int ret = 0;
  int n = 0;
  for (int i = s; n < N; i = (i + 1) % 3) {
    bool enter;
    do {
      enter = false;
      for (int j = 1; j <= N; ++j)
        if (deg[j] == 0 && !mark[j] && i + 1 == c[j]) {
          mark[j] = true;
          for (int k = 1; k <= N; ++k)
            if (adj[j][k] && !mark[k]) {
              --deg[k];
              if (deg[k] == 0) enter = true;
            }
          ++ret;
          ++n;
        }
    } while (enter);
    if (n < N) ++ret;
  }
  memcpy(deg, tmp, sizeof deg);
  return ret;
}
int main() {
  int v;
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) scanf("%d", c + i);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &deg[i]);
    for (int j = 0; j < deg[i]; ++j) {
      scanf("%d", &v);
      adj[v][i] = true;
    }
  }
  int best = 1 << 30;
  for (int s = 0; s < 3; ++s) best = min(best, solve(s));
  printf("%d\n", best);
  return 0;
}
