#include <bits/stdc++.h>
using namespace std;
int n, c[205], du[205], degree[205];
bool vis[205];
vector<int> a[205];
int work(int ci) {
  queue<int> q;
  int tot = 0, sum = 0;
  memset(vis, 0, sizeof(vis));
  while (tot < n) {
    for (int i = 1; i <= n; i++) {
      if (!du[i] && c[i] == ci) {
        q.push(i);
        du[i] = -1;
        tot++;
        sum++;
      }
    }
    while (!q.empty()) {
      int t = q.front();
      q.pop();
      for (int i = 0; i < a[t].size(); i++) {
        int tmp = a[t][i];
        du[tmp]--;
        if (!du[tmp] && c[tmp] == ci) {
          q.push(tmp);
          du[tmp] = -1;
          tot++;
          sum++;
        }
      }
    }
    ci = (ci % 3) + 1;
    sum++;
  }
  return sum;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (int i = 1; i <= n; i++) {
    int k;
    scanf("%d", &k);
    degree[i] = k;
    for (int j = 1; j <= k; j++) {
      int t;
      scanf("%d", &t);
      a[t].push_back(i);
    }
  }
  int ans = 0x7fffffff;
  for (int i = 1; i <= n; i++) du[i] = degree[i];
  ans = min(ans, work(1));
  for (int i = 1; i <= n; i++) du[i] = degree[i];
  ans = min(ans, work(2));
  for (int i = 1; i <= n; i++) du[i] = degree[i];
  ans = min(ans, work(3));
  printf("%d\n", ans - 1);
  return 0;
}
