#include <bits/stdc++.h>
using namespace std;
const int MAXN = 222, inf = 1023456789;
int N, dependencies[MAXN], loc[MAXN];
vector<int>
requires[MAXN], go[3];
int dep[MAXN];
vector<int> q[3];
int solve(int pos) {
  for (int i = 1; i <= N; i++) dep[i] = dependencies[i];
  for (int i = 1; i <= 3; i++) q[i] = go[i];
  int done = 0, used = -1;
  while (done < N) {
    used++;
    if (q[pos].size() > 0) {
      for (int i = 0; i < q[pos].size(); i++) {
        done++;
        for (int j = 0; j < requires[q[pos][i]].size(); j++) {
          dep[requires[q[pos][i]][j]]--;
          if (dep[requires[q[pos][i]][j]] == 0)
            q[loc[requires[q[pos][i]][j]]].push_back(requires[q[pos][i]][j]);
        }
      }
      q[pos].clear();
    }
    pos++;
    if (pos > 3) pos = 1;
  }
  return used;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", &loc[i]);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &dependencies[i]);
    for (int j = 0; j < dependencies[i]; j++) {
      int x;
      scanf("%d", &x);
      requires[x].push_back(i);
    }
    if (dependencies[i] == 0) go[loc[i]].push_back(i);
  }
  int answer = inf;
  answer = min(answer, solve(1));
  answer = min(answer, solve(2));
  answer = min(answer, solve(3));
  printf("%d\n", answer + N);
  return 0;
}
