#include <bits/stdc++.h>
using namespace std;
bool maze[205][205];
int deg[205], mdeg[205];
bool vis[205];
int r[205];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);
    r[i]--;
  }
  memset(maze, false, sizeof(maze));
  memset(deg, 0, sizeof(deg));
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      int p;
      scanf("%d", &p);
      maze[i][p - 1] = true;
      deg[i]++;
    }
  }
  memcpy(mdeg, deg, sizeof(deg));
  int ans = 0x7fffffff;
  for (int s = 0; s < 3; s++) {
    int tmp = 0;
    int cnt = 0;
    memset(vis, false, sizeof(vis));
    memcpy(deg, mdeg, sizeof(mdeg));
    int rm = s;
    while (true) {
      for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        if (deg[i] == 0 && r[i] == rm) {
          for (int j = 0; j < n; j++) {
            if (vis[j]) continue;
            if (maze[j][i]) deg[j]--;
          }
          vis[i] = true;
          i = -1;
          cnt++;
          tmp++;
        }
      }
      if (cnt == n) break;
      tmp++;
      rm++;
      if (rm > 2) rm = 0;
    }
    if (tmp < ans) ans = tmp;
  }
  printf("%d\n", ans);
}
