#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n, m[4][4], a[222], ans, hw[222], tmp, bc, beg[222], st, d[222], v, tt, to,
    cw[222], len, cur;
vector<int> g[222];
bool w[222], can[222];
int main() {
  memset(m, 0, sizeof(m));
  m[1][2] = 1;
  m[1][3] = 2;
  m[2][1] = 2;
  m[2][3] = 1;
  m[3][1] = 1;
  m[3][2] = 2;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  ans = INF;
  memset(hw, 0, sizeof(hw));
  bc = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", hw + i);
    for (int j = 0; j < hw[i]; ++j) {
      scanf("%d", &tmp);
      g[tmp].push_back(i);
    }
    if (!hw[i]) beg[bc++] = i;
  }
  for (int k = 0; k < bc; ++k) {
    st = beg[k];
    memset(w, 0, sizeof(w));
    memset(can, 0, sizeof(can));
    for (int i = 0; i < bc; ++i) can[beg[i]] = true;
    for (int i = 1; i <= n; ++i) cw[i] = hw[i];
    memset(d, INF, sizeof(d));
    d[st] = 0;
    for (int _ = 0; _ < n; ++_) {
      v = 0;
      for (int i = 1; i <= n; ++i)
        if (!w[i] && can[i] && d[i] < d[v]) v = i;
      w[v] = true;
      tt = (int)g[v].size();
      for (int i = 0; i < tt; ++i) {
        to = g[v][i];
        --cw[to];
        if (!cw[to]) can[to] = true;
      }
      for (int i = 1; i <= n; ++i)
        if (can[i]) {
          len = m[a[v]][a[i]];
          d[i] = min(d[i], d[v] + len);
        }
    }
    cur = 0;
    for (int i = 1; i <= n; ++i) cur = max(cur, d[i]);
    ans = min(ans, cur);
  }
  printf("%d", ans + n);
  return 0;
}
