#include <bits/stdc++.h>
using namespace std;
vector<int> to[1231];
int a[1231];
int qiu(int k, int n, int ss[1231]) {
  int s[1231];
  for (int i = 1; i <= n; i++) s[i] = ss[i];
  queue<int> que[5];
  for (int i = 1; i <= n; i++)
    if (s[i] == 0) que[a[i]].push(i);
  int da = 0;
  for (int i = k; n; i = i % 3 + 1) {
    while (que[i].size()) {
      int e = que[i].front();
      n--;
      que[i].pop();
      for (int i = 0; i < to[e].size(); i++) {
        if (--s[to[e][i]] == 0) {
          que[a[to[e][i]]].push(to[e][i]);
        }
      }
    }
    if (n) da++;
  }
  return da;
}
int main() {
  int n;
  while (cin >> n) {
    int s[1231] = {0};
    for (int i = 1; i <= n; i++) cin >> a[i], to[i].clear();
    for (int i = 1; i <= n; i++) {
      int m;
      cin >> m;
      s[i] = m;
      while (m--) {
        int z;
        cin >> z;
        to[z].push_back(i);
      }
    }
    int da = 1e9;
    for (int z = 1; z < 4; z++) da = min(da, qiu(z, n, s));
    cout << da + n << endl;
  }
}
