#include <bits/stdc++.h>
using namespace std;
int pos[205], n, ret;
vector<int> pre[205];
bool vis[205];
bool pre_check(int index) {
  for (int i = 0; i < pre[index].size(); i++)
    if (!vis[pre[index][i]]) return false;
  return true;
}
bool all_complete() {
  for (int i = 1; i <= n; i++)
    if (!vis[i]) return false;
  return true;
}
void work(int now, int cost) {
  while (true) {
    bool find = false;
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      if (pre_check(i) && pos[i] == now) {
        vis[i] = true;
        find = true;
        cost++;
      }
    }
    if (!find) break;
  }
  if (all_complete()) {
    ret = min(ret, cost);
    return;
  }
  work((now % 3) + 1, cost + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &pos[i]);
  for (int i = 1; i <= n; i++) {
    int num;
    scanf("%d", &num);
    for (int j = 1; j <= num; j++) {
      int a;
      scanf("%d", &a);
      pre[i].push_back(a);
    }
  }
  ret = 0x73737373;
  for (int i = 1; i <= 3; i++) {
    memset(vis, false, sizeof(vis));
    vis[0] = true;
    work(i, 0);
  }
  printf("%d\n", ret);
  return 0;
}
