#include <bits/stdc++.h>
using namespace std;
int cost[3][3] = {0, 1, 2, 2, 0, 1, 1, 2, 0};
int N;
int a[250];
int d[250][250];
int in[250], _in[250];
bool h[250];
int k, col, u, ans, best;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", a + i);
  memset(d, 0, sizeof(d));
  memset(in, 0, sizeof(in));
  for (int i = 1; i <= N; i++) {
    scanf("%d", &k);
    while (k--) {
      scanf("%d", &u);
      d[u][i]++;
      in[i]++;
    }
  }
  best = 1 << 20;
  for (int c = 1; c <= 3; c++) {
    memset(h, 0, sizeof(h));
    memcpy(_in, in, sizeof(int) * (N + 1));
    col = c;
    ans = 0;
    for (int v = 1; v <= N; v++) {
      int m_incol = 3, p;
      for (int i = 1; i <= N; i++)
        if ((!h[i]) && (_in[i] == 0) && (cost[col - 1][a[i] - 1] < m_incol)) {
          m_incol = cost[col - 1][a[i] - 1];
          p = i;
        }
      for (int i = 1; i <= N; i++)
        if (d[p][i]) _in[i]--;
      h[p] = 1;
      ans += cost[col - 1][a[p] - 1] + 1;
      col = a[p];
    }
    if (ans < best) best = ans;
  }
  printf("%d\n", best);
}
