#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200 + 10;
int n, deg[MAXN], c[MAXN], l, ans = MAXN * MAXN * MAXN;
bool isMarked[MAXN];
vector<int> mat[MAXN];
queue<int> q[3];
void input() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
    c[i]--;
  }
  for (int i = 1; i <= n; ++i) {
    int k, x;
    cin >> k;
    for (int j = 0; j < k; ++j) {
      cin >> x;
      mat[x].push_back(i);
    }
  }
}
int main() {
  input();
  for (int start = 0; start < 3; ++start) {
    for (int i = 1; i <= n; ++i)
      for (int j = 0; j < (int)mat[i].size(); ++j) deg[mat[i][j]]++;
    for (int i = 1; i <= n; ++i)
      if (!deg[i]) q[c[i]].push(i);
    l = n;
    int now = start, temp = 0;
    while (l) {
      while (q[now].size()) {
        int x = q[now].front();
        q[now].pop();
        isMarked[x] = true;
        for (int i = 0; i < (int)mat[x].size(); ++i)
          if (!isMarked[mat[x][i]]) {
            deg[mat[x][i]]--;
            if (deg[mat[x][i]] == 0) {
              q[c[mat[x][i]]].push(mat[x][i]);
              isMarked[mat[x][i]] = true;
            }
          }
        l--;
      }
      if (l) {
        temp++;
        now = (now + 1) % 3;
      }
    }
    ans = min(ans, temp);
    memset(isMarked, false, sizeof(isMarked));
  }
  cout << ans + n << endl;
  return 0;
}
