#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
int n, c[200];
vector<int> G[200];
bool vis[200];
int dfs(int now) {
  while (1) {
    bool changed = false;
    for (int u = 0; u < (n); u++)
      if (!vis[u] && c[u] == now) {
        bool ok = true;
        for (int i = 0; i < (G[u].size()); i++) {
          int v = G[u][i];
          if (!vis[v]) ok = false;
        }
        if (ok) {
          vis[u] = true;
          changed = true;
        }
      }
    if (!changed) break;
  }
  if (count(vis, vis + n, true) == n) return 0;
  return 1 + dfs((now + 1) % 3);
}
int main() {
  scanf("%d", &n);
  for (int u = 0; u < (n); u++) scanf("%d", c + u), c[u]--;
  for (int u = 0; u < (n); u++) {
    G[u].clear();
    int m;
    scanf("%d", &m);
    for (int i = 0; i < (m); i++) {
      int v;
      scanf("%d", &v);
      v--;
      G[u].push_back(v);
    }
  }
  int ans = INF;
  for (int start = 0; start < (3); start++) {
    for (int u = 0; u < (n); u++) vis[u] = false;
    ans = min(ans, dfs(start) + n);
  }
  printf("%d\n", ans);
  return 0;
}
