#include <bits/stdc++.h>
using namespace std;
int n;
int *comp;
vector<int> *befor;
int t[3][3] = {{0, 1, 2}, {2, 0, 1}, {1, 2, 0}};
map<string, int> ht;
string emp_str() { return string(n, '0'); }
int *can_do(string solved, int on) {
  int *res1 = new int[n];
  for (int i = 0; i < n; i++) res1[i] = 0;
  for (int i = 0; i < n; i++) {
    if (solved[i] == '1' || comp[i] != on) continue;
    bool f = true;
    for (int j = 0; j < befor[i].size(); j++)
      if (solved[befor[i][j]] == '0' && comp[befor[i][j]] != on) f = false;
    if (f) res1[i] = 1;
  }
  int *res2 = new int[n];
  for (int i = 0; i < n; i++) res2[i] = 0;
  for (int i = 0; i < n; i++) {
    if (res1[i] == 0) continue;
    bool f = true;
    for (int j = 0; j < befor[i].size(); j++)
      if ((solved[befor[i][j]] == '0' && comp[befor[i][j]] == on &&
           res1[befor[i][j]] == 0))
        f = false;
    if (f) res2[i] = 1;
  }
  delete[] res1;
  return res2;
}
int do_best(string solved, int on) {
  if (ht.count(solved + (char)(on)) != 0) return ht[solved + (char)(on)];
  int c = 0;
  for (int i = 0; i < n; i++)
    if (solved[i] == '1') c++;
  if (c == n) return 0;
  int min_t = 32000;
  for (int i = 0; i < 3; i++) {
    int *can = can_do(solved, i);
    c = 0;
    for (int j = 0; j < n; j++)
      if (can[j] == 1) c++;
    if (c == 0) continue;
    string n_solved = emp_str();
    for (int j = 0; j < n; j++)
      if (solved[j] == '1' || can[j] == 1) n_solved[j] = '1';
    int tt = do_best(n_solved, i) + t[on][i] + c;
    if (tt < min_t) min_t = tt;
    delete[] can;
  }
  ht[solved + (char)(on)] = min_t;
  return min_t;
}
int main() {
  cin >> n;
  comp = new int[n];
  befor = new vector<int>[n];
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    comp[i] = k - 1;
  }
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int c;
      cin >> c;
      befor[i].push_back(c - 1);
    }
  }
  string solved = emp_str();
  int min_t = 32000;
  for (int i = 0; i < 3; i++) {
    int *can = can_do(solved, i);
    int c = 0;
    for (int j = 0; j < n; j++)
      if (can[j] == 1) c++;
    if (c == 0) continue;
    string n_solved = emp_str();
    for (int j = 0; j < n; j++)
      if (solved[j] == '1' || can[j] == 1) n_solved[j] = '1';
    int tt = do_best(n_solved, i) + c;
    if (tt < min_t) min_t = tt;
    delete[] can;
  }
  cout << min_t << endl;
}
