#include <bits/stdc++.h>
using namespace std;
const int dcost[3][3] = {{0, 1, 2}, {2, 0, 1}, {1, 2, 0}};
struct Part {
  int need;
  int pnum;
  int ps[205];
} p[205];
int ret = 605, n;
bool vis[205];
bool all_ok() {
  for (int i = 1; i <= n; i++)
    if (!vis[i]) return false;
  return true;
}
bool check(int ind) {
  for (int i = 0; i < p[ind].pnum; i++) {
    int pid = p[ind].ps[i];
    if (!vis[pid]) return false;
  }
  return true;
}
void dfs(int now_pos, int cost) {
  while (true) {
    bool change = false;
    for (int i = 1; i <= n; i++)
      if (!vis[i] && p[i].need == now_pos && check(i)) {
        vis[i] = true;
        change = true;
      }
    if (change == false) break;
  }
  if (all_ok()) {
    ret = min(ret, cost);
    return;
  }
  int next = (now_pos + 1) % 3;
  dfs(next, cost + dcost[now_pos][next]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i].need);
    p[i].need--;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i].pnum);
    for (int j = 0; j < p[i].pnum; j++) scanf("%d", &p[i].ps[j]);
  }
  for (int i = 0; i < 3; i++) {
    memset(vis, false, sizeof(vis));
    dfs(i, 0);
  }
  printf("%d\n", ret + n);
  return 0;
}
