#include <bits/stdc++.h>
using namespace std;
bitset<210> need[210];
int comp[210], n;
int start(int pos) {
  bitset<210> was;
  int ans(0);
  while (was.count() != n) {
    for (int j(1); j <= n; ++j)
      for (int i(1); i <= n; ++i)
        if (!was[i] and comp[i] == pos and (need[i] & was) == need[i]) {
          was[i] = true;
          ++ans;
        }
    if (was.count() == n) return ans;
    ++ans;
    ++pos;
    pos %= 3;
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i(1); i <= n; ++i) {
    cin >> comp[i];
    --comp[i];
  }
  for (int i(1); i <= n; ++i) {
    int k;
    cin >> k;
    while (k--) {
      int c;
      cin >> c;
      need[i][c] = true;
    }
  }
  cout << min({start(0), start(1), start(2)});
}
