#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
char buf[1000];
string nextLine(int length = 100) {
  cin.getline(buf, length);
  string s(buf);
  return s;
}
string next(int length = 100) {
  string tmp;
  cin >> tmp;
  return tmp;
}
int nextInt() {
  int tmp;
  scanf("%d", &tmp);
  return tmp;
}
const int MAXN = 210;
vector<int> dep[MAXN];
vector<int> computer[3];
bool completed[MAXN];
int n;
int check(int start) {
  int tot = 0, done = 0;
  for (int i = 0, _i = (MAXN); i < _i; ++i) completed[i] = false;
  while (done < n) {
    bool found = true;
    while (found) {
      found = false;
      for (__typeof((computer[start]).begin()) i = (computer[start]).begin(),
                                               _i = (computer[start]).end();
           i != _i; ++i) {
        if (!completed[*i]) {
          bool cando = true;
          for (__typeof((dep[*i]).begin()) j = (dep[*i]).begin(),
                                           _j = (dep[*i]).end();
               j != _j; ++j) {
            if (!completed[*j]) {
              cando = false;
              break;
            }
          }
          if (cando) {
            done++;
            completed[*i] = true;
            found = true;
          }
        }
      }
    }
    if (done < n) {
      tot++;
      start = (start + 1) % 3;
    }
  }
  return tot;
}
int main() {
  n = nextInt();
  for (int i = 0, _i = (n); i < _i; ++i) {
    computer[nextInt() - 1].push_back(i);
  }
  for (int i = 0, _i = (n); i < _i; ++i) {
    int d = nextInt();
    for (int j = 0, _j = (d); j < _j; ++j) {
      dep[i].push_back(nextInt() - 1);
    }
  }
  cout << n + min(check(0), min(check(1), check(2))) << endl;
}
