#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[205], i, j, k, p, r, x, y, z, b[205], c[205][205];
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i], b[i] = 0;
  for (i = 1; i <= n; i++) {
    cin >> c[i][0];
    for (j = 1; j <= c[i][0]; j++) cin >> c[i][j];
  }
  for (x = 1; x <= 3; x++) {
    y = x;
    k = 0;
    while (1) {
      p = 1;
      do {
        z = 0;
        for (i = 1; i <= n; i++) {
          if (b[i] == 0) {
            p = 0;
            if (a[i] == y) {
              for (j = 1; j <= c[i][0]; j++)
                if (b[c[i][j]] == 0) break;
              if (j > c[i][0]) b[i] = 1, z = 1;
            }
          }
        }
      } while (z);
      if (p) break;
      k++;
      y = (y % 3) + 1;
    }
    for (i = 1; i <= n; i++) b[i] = 0;
    if (x == 1)
      r = k;
    else
      r = min(r, k);
  }
  cout << r + n - 1;
  return 0;
}
