#include <bits/stdc++.h>
using namespace std;
int n, c[210];
int a[210][210];
int co[5][5];
int in[210];
bool vis[210];
int gao(int s) {
  memset(in, 0, sizeof(in));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j]) in[j]++;
  memset(vis, 0, sizeof(vis));
  int now = s;
  int ans = 0;
  while (1) {
    bool flag1 = 0;
    bool flag2 = 0;
    for (int i = 1; i <= n; i++)
      if (in[i] == 0 && !vis[i]) {
        flag1 = 1;
        if (c[i] == now) {
          flag2 = 1;
          vis[i] = 1;
          for (int j = 1; j <= n; j++)
            if (a[i][j]) in[j]--;
        }
      }
    if (flag1 == 0) break;
    if (flag2 == 0) {
      now++;
      if (now == 4) now = 1;
      ans++;
    }
  }
  return ans;
}
int main() {
  co[1][2] = 1, co[1][3] = 2;
  co[2][1] = 2, co[2][3] = 1;
  co[3][1] = 1, co[3][2] = 2;
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    memset(a, 0, sizeof(a));
    for (int i = 1; i <= n; i++) {
      int k;
      scanf("%d", &k);
      while (k--) {
        int j;
        scanf("%d", &j);
        a[j][i] = 1;
      }
    }
    int ans = gao(1);
    ans = min(ans, gao(2));
    ans = min(ans, gao(3));
    ans += n;
    printf("%d\n", ans);
  }
}
