#include <bits/stdc++.h>
using namespace std;
int ans = 1 << 30;
bool done[205];
int loc[205];
int M[205][205];
int n;
void DFS(int pos, int left, int cur = 0) {
  bool undone = true;
  while (undone) {
    undone = false;
    for (int i = 1; i <= n; ++i) {
      if (loc[i] != pos) {
        continue;
      }
      if (!done[i]) {
        bool ok = true;
        for (int j = 1; j <= M[i][0]; ++j) {
          if (!done[M[i][j]]) {
            ok = false;
            break;
          }
        }
        if (ok) {
          --left;
          done[i] = true;
          undone = true;
        }
      }
    }
  }
  if (!left) {
    ans = min(ans, cur + n);
  } else {
    DFS((pos + 1) % 3, left, cur + 1);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> loc[i];
    --loc[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> M[i][0];
    for (int j = 0; j < M[i][0]; ++j) {
      int x;
      cin >> x;
      M[i][j + 1] = x;
    }
  }
  for (int start = 0; start < 3; ++start) {
    memset(done, 0, sizeof done);
    DFS(start, n);
  }
  cout << ans << endl;
  return 0;
}
