#include <bits/stdc++.h>
using namespace std;
inline int rdi() {
  int d;
  scanf("%d", &d);
  return d;
}
inline char rdc() {
  scanf(" ");
  return getchar();
}
inline string rds() {
  string s;
  cin >> s;
  return s;
}
inline double rddb() {
  double d;
  scanf("%lf", &d);
  return d;
}
template <class T>
inline bool updateMin(T& a, T b) {
  return a > b ? a = b, true : false;
}
template <class T>
inline bool updateMax(T& a, T b) {
  return a < b ? a = b, true : false;
}
const int N = 256;
queue<int> q;
vector<int> to[N];
int n, computer[N], ind[N], indeg[N];
int BFS(int c) {
  memcpy(ind, indeg, sizeof ind);
  while (!q.empty()) q.pop();
  for (int u = (1); u <= (n); ++u)
    if (!ind[u]) q.push(u);
  int sum = 0, cnt = 0;
  for (; !q.empty(); q.pop()) {
    if (++cnt > q.size()) cnt = 1, ++sum, c = c % 3 + 1;
    int u = q.front();
    if (computer[u] != c) {
      q.push(u);
      continue;
    }
    cnt = 0;
    for (int i = 0; i < to[u].size(); ++i) {
      int v = to[u][i];
      if (!--ind[v]) q.push(v), cnt = 0;
    }
  }
  return sum + n;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> computer[i];
  for (int v = 1; v <= n; ++v) {
    int m;
    scanf("%d", &m);
    for (int j = 0; j < m; ++j) {
      int u;
      scanf("%d", &u);
      to[u].push_back(v), ++indeg[v];
    }
  }
  cout << min(BFS(1), min(BFS(2), BFS(3))) << endl;
  return 0;
}
