#include <bits/stdc++.h>
using namespace std;
vector<int> mp[15000];
int d[5][5];
int a[2500];
int degree[2500];
int n;
int Slove(int ss) {
  queue<int> s;
  int ans = n;
  int cont = 0;
  int now = ss;
  while (1) {
    while (1) {
      int flag = 0;
      for (int i = 1; i <= n; i++) {
        if (degree[i] == 0 && a[i] == now) {
          flag = 1;
          degree[i] = -1;
          cont++;
          for (int j = 0; j < mp[i].size(); j++) {
            int v = mp[i][j];
            degree[v]--;
          }
        }
      }
      if (flag == 0) break;
    }
    if (cont == n) break;
    now++;
    ans++;
    if (now == 4) now = 1;
  }
  return ans;
}
int temp[15000];
int main() {
  d[1][1] = 0;
  d[1][2] = 1;
  d[1][3] = 2;
  d[2][1] = 2;
  d[2][2] = 1;
  d[2][3] = 1;
  d[3][1] = 1;
  d[3][2] = 1;
  d[3][3] = 0;
  while (~scanf("%d", &n)) {
    memset(degree, 0, sizeof(degree));
    for (int i = 1; i <= n; i++) mp[i].clear();
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
      int ki;
      scanf("%d", &ki);
      for (int j = 1; j <= ki; j++) {
        int x;
        scanf("%d", &x);
        mp[x].push_back(i);
        degree[i]++;
      }
    }
    for (int i = 1; i <= n; i++) temp[i] = degree[i];
    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++) degree[i] = temp[i];
    ans = min(ans, Slove(1));
    for (int i = 1; i <= n; i++) degree[i] = temp[i];
    ans = min(ans, Slove(2));
    for (int i = 1; i <= n; i++) degree[i] = temp[i];
    ans = min(ans, Slove(3));
    printf("%d\n", ans);
  }
}
