#include <bits/stdc++.h>
using namespace std;
int t;
int n;
int c[300];
vector<int> p[300];
int ch[300];
int ch_tmp[300];
int ost;
queue<int> zero;
int ans = 1000000000;
bool used[300];
int main() {
  cin >> n;
  ost = n;
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 1; j <= k; j++) {
      int a;
      cin >> a;
      p[a].push_back(i);
      ch[i]++;
    }
  }
  for (int s = 1; s <= 3; s++) {
    if (0) cerr << "~~~~~try #" << s << "~~~~~\n";
    ost = n;
    t = 0;
    for (int i = 1; i <= n; i++) {
      ch_tmp[i] = ch[i];
      used[i] = 0;
    }
    for (int cur = s; ost > 0; cur = cur % 3 + 1) {
      while (!zero.empty()) zero.pop();
      for (int i = 1; i <= n; i++)
        if (c[i] == cur && ch_tmp[i] == 0 && !used[i]) zero.push(i);
      while (!zero.empty()) {
        ost--;
        for (int i = 0; i < p[zero.front()].size(); i++) {
          ch_tmp[p[zero.front()][i]]--;
          if (ch_tmp[p[zero.front()][i]] == 0 && c[p[zero.front()][i]] == cur &&
              !used[p[zero.front()][i]])
            zero.push(p[zero.front()][i]);
        }
        if (0) cerr << "removing " << zero.front() << "\n";
        used[zero.front()] = 1;
        zero.pop();
        t++;
      }
      if (0) cerr << cur << " --> " << cur % 3 + 1 << endl;
      t++;
    }
    t--;
    if (0) cerr << t << "--\n";
    ans = min(ans, t);
  }
  cout << ans;
}
