#include <bits/stdc++.h>
using namespace std;
int n;
int a[256];
vector<int> b[256], c[256];
int dead[256];
void read() {
  int i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &j);
    int x;
    while (j--) {
      scanf("%d", &x);
      b[i].push_back(x);
    }
  }
}
int go(int x) {
  int left = n;
  int ch;
  int i, j;
  int ans = 0;
  for (i = 1; i <= n; i++) dead[i] = 0;
  while (left) {
    ch = 0;
    for (i = 1; i <= n; i++) {
      if (!dead[i] && a[i] == x) {
        for (j = 0; j < (int)c[i].size(); j++) {
          if (!dead[c[i][j]]) {
            break;
          }
        }
        if (j == (int)c[i].size()) {
          dead[i] = 1;
          --left;
          ch = 1;
        }
      }
    }
    if (!ch) {
      ++ans;
      x++;
      if (x == 4) x = 1;
    }
  }
  return ans;
}
void solve() {
  int i, j;
  int ans = 10000;
  for (i = 1; i <= 3; i++) {
    for (j = 1; j <= n; j++) c[j] = b[j];
    ans = min(ans, go(i));
  }
  printf("%d\n", ans + n);
}
int main() {
  read();
  solve();
  return 0;
}
