#include <bits/stdc++.h>
const int MaxN = 1000;
using namespace std;
vector<int> G[MaxN + 5];
int inx[MaxN + 5], ind[MaxN + 5], vis[MaxN + 5];
int c[MaxN + 5];
int n, tot = 0;
int Solve(int k) {
  tot = 0;
  queue<int> q[10];
  int step = -1;
  for (int i = 1; i <= n; i++) {
    ind[i] = inx[i];
    if (inx[i] == 0) q[c[i]].push(i);
  }
  while (q[1].size() || q[2].size() || q[3].size()) {
    step++;
    while (q[k].size()) {
      step++;
      int now = q[k].front();
      q[k].pop();
      int all = G[now].size();
      for (int i = 0; i < all; i++) {
        int v = G[now][i];
        if (--ind[v] == 0) {
          q[c[v]].push(v);
        }
      }
    }
    if (k == 1)
      k = 2;
    else if (k == 2)
      k = 3;
    else if (k == 3)
      k = 1;
  }
  if (step == -1) return 0;
  return step;
}
int main() {
  while (~scanf("%d", &n)) {
    memset(inx, 0, sizeof(inx));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &c[i]);
    }
    for (int i = 1; i <= n; i++) {
      int x, v;
      scanf("%d", &x);
      for (int j = 1; j <= x; j++) {
        scanf("%d", &v);
        G[v].push_back(i);
        inx[i]++;
      }
    }
    int ans = 1 << 30;
    for (int i = 1; i <= 3; i++) {
      int tmp = Solve(i);
      ans = min(ans, tmp);
    }
    cout << ans << endl;
  }
}
