#include <bits/stdc++.h>
using namespace std;
int N;
int C[204];
vector<int> adj[204];
int go(int start, vector<queue<int> > q, vector<int> in_deg) {
  int res = 0;
  int h = start;
  while (true) {
    int d;
    for (d = 0; d < 3 && q[h].empty(); ++d, h = (h + 1) % 3)
      ;
    if (d >= 3) break;
    res += d;
    int u = q[h].front();
    q[h].pop();
    res++;
    for (int j = 0; j < adj[u].size(); ++j) {
      int v = adj[u][j];
      if (--in_deg[v] == 0) q[C[v]].push(v);
    }
  }
  return res;
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> C[i];
    --C[i];
  }
  for (int v = 0; v < N; ++v) {
    int k;
    cin >> k;
    while (k-- > 0) {
      int u;
      cin >> u;
      --u;
      adj[u].push_back(v);
    }
  }
  vector<int> in_deg(N);
  for (int u = 0; u < N; ++u) {
    for (int j = 0; j < (int)adj[u].size(); ++j) {
      int v = adj[u][j];
      ++in_deg[v];
    }
  }
  vector<queue<int> > q(3);
  for (int u = 0; u < N; ++u) {
    if (in_deg[u] == 0) q[C[u]].push(u);
  }
  int res = 1000000000;
  for (int start = 0; start < 3; ++start) {
    int cur = go(start, q, in_deg);
    res = min(res, cur);
  }
  cout << res << endl;
  return 0;
}
