#include <bits/stdc++.h>
using namespace std;
int n, c[300], b[300][300], v[300], p[300], ans = 100000000, tot;
int s[500], q[4][500];
void Work(int x) {
  int pos = x, cost = 0, res = n;
  for (int i = 1; i <= 3; i++) s[i] = 0;
  for (int i = 0; i < s[0]; i++) {
    int h = q[0][i];
    q[c[h]][s[c[h]]++] = h;
  }
  for (int i = 0; i < n; i++) p[i] = v[i];
  while (res > 0) {
    while (s[pos] > 0) {
      s[pos]--;
      res--;
      int h = q[pos][s[pos]];
      for (int i = 0; i < n; i++) {
        if (b[h][i]) {
          p[i]--;
          if (p[i] == 0) q[c[i]][s[c[i]]++] = i;
        }
      }
    }
    if (res) {
      cost++;
      pos++;
      pos = pos > 3 ? 1 : pos;
    }
  }
  if (cost < ans) ans = cost;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
  for (int i = 0; i < n; i++) {
    int pos;
    scanf("%d", &v[i]);
    if (v[i] == 0) q[0][s[0]++] = i;
    for (int j = 0; j < v[i]; j++) {
      scanf("%d", &pos);
      b[--pos][i] = 1;
    }
  }
  for (int i = 1; i <= 3; i++) Work(i);
  printf("%d", ans + n);
  return 0;
}
