#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
const int inf_int = 1e9 + 5;
const ll inf_ll = 1e18 + 5;
const int NMax = 2e2 + 5;
vector<int> adj[NMax];
int test(int startComputer, int N, vector<int> inDeg,
         const vector<int>& computer) {
  queue<int> canDoComp[4];
  for (int i = 1; i <= N; ++i) {
    if (inDeg[i] == 0) {
      canDoComp[computer[i]].push(i);
    }
  }
  int comp = startComputer, numDone = 0, totalTime = 0;
  while (numDone < N) {
    while (canDoComp[comp].size()) {
      int part = canDoComp[comp].front();
      canDoComp[comp].pop();
      ++numDone;
      totalTime += 1;
      for (int nxt : adj[part]) {
        --inDeg[nxt];
        if (inDeg[nxt] == 0) {
          canDoComp[computer[nxt]].push(nxt);
        }
      }
    }
    comp = (comp == 3) ? 1 : comp + 1;
    totalTime += 1;
  }
  return totalTime - 1;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> computer(N + 1), inDeg(N + 1);
  for (int i = 1; i <= N; ++i) {
    cin >> computer[i];
  }
  for (int i = 1; i <= N; ++i) {
    int k;
    cin >> k;
    while (k--) {
      int x;
      cin >> x;
      adj[x].push_back(i);
      ++inDeg[i];
    }
  }
  int ans = test(1, N, inDeg, computer);
  ans = min(ans, test(2, N, inDeg, computer));
  ans = min(ans, test(3, N, inDeg, computer));
  cout << ans << '\n';
  return 0;
}
