#include <bits/stdc++.h>
using namespace std;
int main() {
  cout.precision(10);
  cout << fixed;
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int> > a(N);
  for (int i = 0; i < N; ++i) {
    int k;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      int x;
      cin >> x;
      --x;
      a[x].push_back(i);
    }
  }
  int ans = 1e9;
  for (int b = 0; b < 3; ++b) {
    int c = b;
    int res = 0;
    vector<int> toUnlock(N);
    for (int i = 0; i < N; ++i) {
      for (size_t j = 0; j < a[i].size(); ++j) {
        ++toUnlock[a[i][j]];
      }
    }
    int left = N;
    while (left > 0) {
      int changed = 1;
      while (changed) {
        changed = 0;
        for (int i = 0; i < N; ++i) {
          if (toUnlock[i] == 0 && p[i] == c) {
            toUnlock[i] = -1;
            ++res;
            changed = 1;
            --left;
            for (size_t j = 0; j < a[i].size(); ++j) {
              --toUnlock[a[i][j]];
            }
          }
        }
      }
      if (left > 0) {
        ++res;
        c = (c + 1) % 3;
      }
    }
    ans = min(ans, res);
  }
  cout << ans << '\n';
  return 0;
}
