#include <bits/stdc++.h>
using namespace std;
vector<int> adj[202];
int ind[202], temp[202], n, v[202], ara[] = {1, 2, 3, 1, 2};
int cst[4][4];
int bfs(int s) {
  queue<int> q[4];
  for (int i = 1; i <= n; i++) {
    temp[i] = ind[i];
    if (!temp[i]) q[v[i]].push(i);
  }
  int now = s, cnt = n, ret = 0;
  while (cnt) {
    while (!q[now].empty()) {
      int x = q[now].front();
      q[now].pop();
      cnt--;
      ret++;
      for (int i = 0; i < adj[x].size(); i++) {
        int y = adj[x][i];
        temp[y]--;
        if (!temp[y]) q[v[y]].push(y);
      }
    }
    for (int i = now - 1; i < 5 && cnt; i++)
      if (!q[ara[i]].empty()) {
        ret += cst[now][ara[i]];
        now = ara[i];
        break;
      }
  }
  return ret;
}
int main() {
  cst[1][2] = 1;
  cst[1][3] = 2;
  cst[2][1] = 2;
  cst[2][3] = 1;
  cst[3][1] = 1;
  cst[3][2] = 2;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (int i = 1; i <= n; i++) {
    int k, x;
    scanf("%d", &k);
    while (k--) {
      scanf("%d", &x);
      adj[x].push_back(i);
      ind[i]++;
    }
  }
  printf("%d\n", min(bfs(1), min(bfs(2), bfs(3))));
  return 0;
}
