#include <bits/stdc++.h>
using namespace std;
vector<int> g[300], g1[300];
queue<int> ww[4], ww1[4];
int n;
int du[300], du1[300];
int v[300];
int main() {
  cin >> n;
  int i, j;
  for (i = 1; i <= n; i++) cin >> v[i];
  int k;
  queue<int> que;
  for (i = 1; i <= n; i++) {
    scanf("%d", &k);
    int p;
    for (j = 0; j < k; j++) {
      scanf("%d", &p);
      g1[p].push_back(i);
      du1[i]++;
    }
    if (k == 0) {
      ww1[v[i]].push(i);
    }
  }
  int s, ss, cnt = 0, ans = 1000000;
  for (ss = 1; ss <= 3; ss++) {
    s = ss;
    cnt = 0;
    for (i = 0; i < 4; i++) {
      while (!ww[i].empty()) ww[i].pop();
    }
    memset(du, 0, sizeof(du));
    for (i = 1; i <= n; i++) {
      g[i].clear();
      for (j = 0; j < g1[i].size(); j++) g[i].push_back(g1[i][j]);
      du[i] = du1[i];
    }
    for (i = 1; i <= n; i++) {
      if (du[i] == 0) ww[v[i]].push(i);
    }
    int temp = 0;
    while (1) {
      while (!ww[s].empty()) {
        k = ww[s].front();
        ww[s].pop();
        for (i = 0; i < g[k].size(); i++) {
          int j = g[k][i];
          du[j]--;
          if (du[j] == 0) ww[v[j]].push(j);
        }
        cnt++;
        ;
      }
      s = ((s) % 3 + 1);
      if (cnt == n) break;
      temp++;
    }
    if (temp < ans) ans = temp;
  }
  cout << ans + n << endl;
  return 0;
}
