#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = 1.0 / 0.0f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MOD = 1000000007;
inline int LC(int x) { return x << 1; }
inline int RC(int x) { return (x << 1) | 1; }
inline bool eq0(double x) { return fabs(x) < eps; }
inline bool eq(double x, double y) { return fabs(x - y) < eps; }
inline bool ls(double x, double y) { return x + eps < y; }
inline bool gr(double x, double y) { return x - eps > y; }
inline bool greq(double x, double y) { return x + eps >= y; }
inline bool lseq(double x, double y) { return x - eps <= y; }
inline double fmax(double x, double y) { return gr(x, y) ? x : y; }
inline double fmin(double x, double y) { return ls(x, y) ? x : y; }
template <class T>
inline T SQR(T x) {
  return x * x;
}
template <class T>
inline int SIZE(const T &x) {
  return x.size();
}
const int MAXN = 210;
int n;
int c[MAXN], deg[MAXN], work[MAXN];
vector<int> e[MAXN];
int run(int st) {
  memcpy(work, deg, sizeof deg);
  int res = 0;
  while (true) {
    int flag = true;
    for (int j = 1; j <= n; j++) {
      if (work[j] >= 0) {
        flag = false;
        break;
      }
    }
    if (flag) break;
    flag = true;
    for (int j = 1; j <= n; j++) {
      if (c[j] == st && work[j] == 0) {
        flag = false;
        for (int k = 0; k < SIZE(e[j]); k++) {
          work[e[j][k]]--;
        }
        work[j] = -1;
      }
    }
    if (flag) {
      st = (st + 1) % 3;
      res++;
    }
  }
  return res;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    c[i]--;
  }
  for (int i = 1; i <= n; i++) {
    cin >> deg[i];
    for (int j = 0; j < deg[i]; j++) {
      int a;
      cin >> a;
      e[a].push_back(i);
    }
  }
  int ans = min(run(0), min(run(1), run(2))) + n;
  cout << ans << endl;
  return 0;
}
