#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, a;
  int b[200 + 5], d[200 + 5], where[200 + 5];
  vector<int> s[3];
  vector<int> e[200 + 5];
  vector<int> st;
  cin >> n;
  fill(d, d + n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a;
    s[a - 1].push_back(i);
    where[i] = a - 1;
  }
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    cin >> k;
    if (!k) {
      st.push_back(where[i]);
    }
    while (k--) {
      cin >> a;
      e[a - 1].push_back(i);
      ++d[i];
    }
  }
  int ans = 1 << 28;
  for (int k = 0; k < st.size(); ++k) {
    int cur = st[k], cnt = 0, tot = 0;
    vector<int> d2(d, d + n);
    fill(b, b + n, 0);
    while (true) {
      queue<int> q;
      for (vector<int>::iterator itr = s[cur].begin(); itr != s[cur].end();
           ++itr) {
        if (!d2[*itr] && !b[*itr]) {
          q.push(*itr);
        }
      }
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        b[u] = 1;
        ++cnt;
        if (cnt == n) {
          goto end;
        }
        for (vector<int>::iterator itr = e[u].begin(); itr != e[u].end();
             ++itr) {
          --d2[*itr];
          if (find(s[cur].begin(), s[cur].end(), *itr) != s[cur].end() &&
              !d2[*itr] && !b[*itr]) {
            q.push(*itr);
          }
        }
      }
      ++cur;
      ++tot;
      cur %= 3;
    }
  end:
    if (cnt == n) {
      ans = min(ans, tot + n);
    }
  }
  cout << ans << endl;
}
