#include <bits/stdc++.h>
using namespace std;
template <class T>
void checkmin(T &t, T x) {
  if (x < t) t = x;
}
template <class T>
void checkmax(T &t, T x) {
  if (x > t) t = x;
}
const int N = 205;
int n;
int a[N][N];
int c[N];
int v[N];
int num[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &c[i]);
  }
  for (int i = 1; i <= n; i++) {
    int x, num;
    scanf("%d", &num);
    for (int j = 1; j <= num; j++) {
      scanf("%d", &x);
      a[x][i] = 1;
    }
  }
  int ans = 0x7FFFFFFF;
  for (int st = 1; st <= 3; st++) {
    int pre = st;
    int now = 0;
    memset(v, 0, sizeof(v));
    memset(num, 0, sizeof(num));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (a[j][i]) num[i]++;
      }
    }
    for (int i = 1; i <= n; i++) {
      bool found = 0;
      for (int j = 1; j <= n; j++) {
        if (!v[j] && !num[j] && c[j] == pre) {
          found = 1;
          now++;
          v[j] = 1;
          for (int k = 1; k <= n; k++) {
            if (a[j][k]) num[k]--;
          }
          break;
        }
      }
      if (found) continue;
      found = 0;
      for (int j = 1; j <= n; j++) {
        if (!v[j] && !num[j] && c[j] == pre % 3 + 1) {
          found = 1;
          now += 2;
          v[j] = 1;
          for (int k = 1; k <= n; k++) {
            if (a[j][k]) num[k]--;
          }
          pre = c[j];
          break;
        }
      }
      if (found) continue;
      found = 0;
      for (int j = 1; j <= n; j++) {
        if (!v[j] && !num[j]) {
          found = 1;
          now += 3;
          v[j] = 1;
          for (int k = 1; k <= n; k++) {
            if (a[j][k]) num[k]--;
          }
          pre = c[j];
          break;
        }
      }
    }
    checkmin(ans, now);
  }
  cout << ans << endl;
}
