#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, nxt;
} e[100000];
int ind[205], tind[205], p[205], cnt;
int c[205], mins, n;
void ini() {
  cnt = 0;
  memset(p, -1, sizeof(p));
}
void addedge(int u, int v) {
  e[cnt].v = v;
  e[cnt].nxt = p[u];
  p[u] = cnt++;
}
int cal(int k) {
  int i, j, res = 0;
  queue<int> q;
  while (1) {
    int flg = 0;
    for (i = 1; i <= n; i++) {
      if (tind[i] == 0) {
        flg = 1;
        if (c[i] == k) {
          q.push(i);
          while (!q.empty()) {
            int u = q.front(), v;
            --tind[u];
            q.pop();
            for (j = p[u]; j != -1; j = e[j].nxt) {
              v = e[j].v;
              --tind[v];
              if (tind[v] == 0 && c[v] == k) q.push(v);
            }
          }
        }
      }
    }
    if (!flg) break;
    ++res;
    k = (k + 1) % 3;
    if (k == 0) k = 3;
  }
  return res;
}
int main() {
  int i, j, tp;
  while (cin >> n) {
    for (i = 1; i <= n; i++) cin >> c[i];
    ini();
    for (i = 1; i <= n; i++) {
      cin >> ind[i];
      for (j = 0; j < ind[i]; j++) {
        int u;
        cin >> u;
        addedge(u, i);
      }
    }
    mins = 0x3fffffff;
    for (i = 1; i <= 3; i++) {
      for (j = 1; j <= n; j++) tind[j] = ind[j];
      tp = cal(i);
      if (tp < mins) mins = tp;
    }
    cout << mins + n - 1 << endl;
  }
  return 0;
}
