#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > Parts(4);
vector<vector<int> > Need;
vector<int> ForNeed;
int main() {
  int n, a, b, c;
  scanf("%d", &n);
  Need.resize(n + 1, vector<int>(n + 1));
  ForNeed.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a);
    Parts[a].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a);
    ForNeed[i] = a;
    for (int j = 0; j < a; ++j) {
      scanf("%d", &b);
      Need[i][b] = Need[b][i] = 1;
    }
  }
  int ans = 100000000, temp;
  for (int i = 1; i <= 3; ++i) {
    vector<int> N2 = ForNeed;
    int ver = i, todo = n;
    temp = 0;
    while (todo > 0) {
      for (int z = 0; z < Parts[ver].size() && todo; ++z) {
        for (int j = 0; j < Parts[ver].size(); ++j) {
          if (N2[Parts[ver][j]] == 0) {
            --todo;
            --N2[Parts[ver][j]];
            for (int k = 1; k <= n; ++k) {
              if (Need[k][Parts[ver][j]]) {
                --N2[k];
              }
            }
          }
        }
      }
      ++ver;
      if (todo == 0) break;
      if (ver == 4) ver = 1;
      ++temp;
    }
    ans = min(ans, temp);
  }
  cout << ans + n;
  return 0;
}
