#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  return out << p.first << ' ' << p.second;
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
    out << *i << ' ';
  }
  return out;
}
template <class T, class U>
istream &operator>>(istream &in, pair<T, U> &p) {
  return in >> p.first >> p.second;
}
template <class T>
istream &operator>>(istream &in, vector<T> &v) {
  for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
    in >> (*i);
  }
  return in;
}
template <class T>
ostream &operator,(ostream &out, const T &t) {
  return out << t;
}
template <class T>
istream &operator,(istream &in, T &t) {
  return in >> t;
}
int io_init() {
  ios_base::sync_with_stdio(false);
  cout, fixed, setprecision(9);
  return 1;
}
int _io_dummy = io_init();
template <class T, class U>
bool remin(T &a, const U &b) {
  return b < a ? a = b, true : false;
}
template <class T, class U>
bool remax(T &a, const U &b) {
  return b > a ? a = b, true : false;
}
bool D[201][201];
int cnt[201];
int solve(int n, int s, vector<int> &room) {
  vector<int> can[4];
  for (int i = (1); i <= (n); ++i) {
    for (int j = (1); j <= (n); ++j) cnt[i] += D[i][j];
    if (cnt[i] == 0) {
      can[room[i - 1]].push_back(i);
    }
  }
  int res = n, elapsed = n;
  while (elapsed > 0) {
    while (!can[s].empty()) {
      int t = can[s].back();
      can[s].pop_back();
      --elapsed;
      for (int j = (1); j <= (n); ++j) {
        if (cnt[j]) {
          cnt[j] -= D[j][t];
          if (cnt[j] == 0) {
            can[room[j - 1]].push_back(j);
          }
        }
      }
    }
    res += elapsed > 0;
    if (++s == 4) s = 1;
  }
  return res;
}
int main() {
  int n;
  cin, n;
  vector<int> room(n), deps;
  cin, room;
  for (int t = (1); t <= (n); ++t) {
    int m, dep;
    cin, m;
    for (int mm = 0; mm < (m); ++mm) {
      cin, dep;
      D[t][dep] = true;
    }
  }
  for (int k = (1); k <= (n); ++k)
    for (int i = (1); i <= (n); ++i)
      for (int j = (1); j <= (n); ++j) {
        D[k][j] |= D[k][i] & D[i][j];
      }
  int best = INF;
  for (int s = (1); s <= (3); ++s) remin(best, solve(n, s, room));
  cout, best, '\n';
}
