#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return a == 0 ? b : gcd(b % a, a);
}
template <class T>
string tostring(T a) {
  ostringstream os;
  os << a;
  return os.str();
}
int toint(string a) {
  istringstream is(a);
  int p;
  is >> p;
  return p;
}
long long toll(string a) {
  istringstream is(a);
  long long p;
  is >> p;
  return p;
}
int need[205][205];
int C[205];
int n;
int cost(int pos) {
  int res = 0;
  vector<bool> f(n, false);
  int fcnt = 0;
  while (fcnt != n) {
    bool changed = true;
    while (changed) {
      changed = false;
      for (int i = 0; i < n; i++) {
        if (f[i]) continue;
        if (C[i] != pos) continue;
        bool ok = true;
        for (int j = 0; j < n; j++) {
          if (need[i][j] && !f[j]) ok = false;
        }
        if (ok) {
          changed = true;
          fcnt++;
          f[i] = true;
        }
      }
    }
    res++;
    pos = (pos + 1) % 3;
  }
  return res + n - 1;
}
int main() {
  memset(need, 0, sizeof(need));
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> C[i];
    C[i] %= 3;
  }
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    for (int j = 0; j < t; j++) {
      int r;
      cin >> r;
      r--;
      need[i][r] = 1;
    }
  }
  int res0 = cost(0);
  int res1 = cost(1);
  int res2 = cost(2);
  cout << min(res0, min(res1, res2));
}
