#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 + 10;
vector<int> adj[maxn], q;
int c[maxn], n, m[maxn];
bool mark[maxn], mark2[maxn];
void dfs(int v) {
  mark[v] = true;
  for (int i = 0; i < adj[v].size(); i++) {
    int tmp = adj[v][i];
    if (!mark[tmp]) dfs(tmp);
  }
  q.push_back(v);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 1; j <= k; j++) {
      int x;
      cin >> x;
      adj[i].push_back(x);
    }
  }
  for (int i = 1; i <= n; i++)
    if (!mark[i]) dfs(i);
  for (int i = 1; i <= 3; i++) {
    int com = i;
    m[i] = n;
    memset(mark2, 0, sizeof mark2);
    for (int bomb = 0; bomb == 0; bomb += 0) {
      for (int j = 0; j < q.size(); j++) {
        int tmp = q[j];
        if (c[tmp] == com && !mark2[tmp]) {
          if (!adj[tmp].size()) mark2[tmp] = true;
          for (int l = 0; l < adj[tmp].size(); l++) {
            int tmp2 = adj[tmp][l];
            if (!mark2[tmp2]) break;
            if (l == adj[tmp].size() - 1) mark2[tmp] = true;
          }
        }
      }
      for (int j = 1; j <= n; j++) {
        if (!mark2[j]) break;
        if (j == n) bomb = 1;
      }
      if (bomb == 0) m[i]++;
      com = (com + 1) % 3;
      if (com == 0) com = 3;
    }
  }
  for (int i = 2; i <= 3; i++) m[1] = min(m[1], m[i]);
  cout << m[1] << endl;
  return 0;
}
