#include <bits/stdc++.h>
using namespace std;
const int inf = (int)2e9;
const long long md = 1000000007;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> deg(n, 0);
  vector<int> com(n, 0);
  for (int i = 0; i < n; i++) cin >> com[i];
  ;
  vector<vector<int> > adj(n);
  for (int i = 0; i < (int)n; i++) {
    int r;
    cin >> r;
    deg[i] += r;
    for (int first = 0; first < (int)r; first++) {
      int v;
      cin >> v;
      adj[v - 1].emplace_back(i);
    }
  }
  long long ans = LLONG_MAX;
  vector<int> on(n, 1);
  vector<int> degOrg(deg);
  for (int st = 1; st <= 3; st++) {
    fill((on).begin(), (on).end(), 1);
    deg = degOrg;
    int alive = n;
    long long loc = 0;
    int stcur = st;
    while (alive) {
      int count = 0;
      do {
        count = 0;
        for (int i = 0; i < (int)n; i++) {
          if (on[i] && !deg[i] && com[i] == stcur) {
            on[i] = 0;
            alive--;
            count++;
            for (int v : adj[i]) deg[v]--;
          }
        }
        loc += count;
      } while (count);
      stcur++;
      if (alive) loc++;
      if (stcur > 3) stcur = 1;
    }
    ans = min(ans, loc);
  }
  cout << ans << endl;
}
