#include <bits/stdc++.h>
using namespace std;
vector<int> pc[4];
bool need[300][300], a[300][300], done[300];
int num[300], b[300];
int main() {
  memset(need, 0, sizeof need);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int tmp;
    cin >> tmp;
    pc[tmp].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> num[i];
    for (int j = 0; j < num[i]; ++j) {
      int tmp;
      cin >> tmp;
      need[i][tmp] = true;
    }
  }
  int ans = 1000 * 1000;
  for (int st = 1; st <= 3; ++st) {
    memset(done, 0, sizeof done);
    for (int i = 1; i <= n; ++i) b[i] = num[i];
    for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= n; ++j) a[i][j] = need[i][j];
    int state = st;
    int nowans = 0;
    bool fin = false;
    while (!fin) {
      bool ch = true;
      while (ch) {
        ch = false;
        for (int i = 0; i < pc[state].size(); ++i) {
          int lev = pc[state][i];
          if (b[lev] == 0 && !done[lev]) {
            done[lev] = true;
            ++nowans;
            for (int j = 1; j <= n; ++j)
              if (a[j][lev]) {
                --b[j];
                a[j][lev] = false;
              }
            ch = true;
          }
        }
      }
      fin = true;
      for (int i = 1; i <= n; ++i)
        if (!done[i]) fin = false;
      if (!fin) {
        ++state;
        ++nowans;
        if (state > 3) state -= 3;
      }
    }
    ans = min(ans, nowans);
  }
  cout << ans;
  return 0;
}
