#include <bits/stdc++.h>
using namespace std;
int c[205];
int d[205];
bool g[205][205];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i) {
    scanf("%d", &c[i]);
  }
  for (int i = 0; i < (int)(n); ++i) {
    int total, p;
    cin >> total;
    for (int j = 0; j < (int)(total); ++j) {
      cin >> p;
      g[p - 1][i] = 1;
      ++d[i];
    }
  }
  int ret = 1000000000;
  for (int cur = (1); cur <= (3); ++cur) {
    int curComp = cur;
    int t[205];
    int cost = 0;
    int total = 0;
    for (int i = 0; i < (int)(n); ++i) t[i] = d[i];
    while (total < n) {
      bool shouldSwitch = true;
      for (int i = 0; i < (int)(n); ++i) {
        if (c[i] == curComp && t[i] == 0) {
          for (int j = 0; j < (int)(n); ++j) {
            if (g[i][j]) {
              --t[j];
              if (c[i] == c[j]) shouldSwitch = false;
            }
          }
          t[i]--;
          total++;
        }
      }
      if (!shouldSwitch) continue;
      curComp++;
      if (curComp > 3) curComp = 1;
      if (total != n) cost++;
    }
    if (cost < ret) ret = cost;
  }
  cout << n + ret << endl;
}
