#include <bits/stdc++.h>
using namespace std;
long long n;
long long comp[201];
struct graph {
  vector<long long> mat[201];
  vector<long long> ind;
  graph() {
    ind.resize(201);
    for (long long i = 0; i < 201; i++) mat[i].resize(201), ind[i] = 0;
  }
};
int tmp[201];
int done;
long long f(long long u, graph &g) {
  if (done == n) return 0;
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (g.ind[i] == 0 && u == comp[i]) q.push(i);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    done++;
    for (int j = 1; j <= n; j++)
      if (g.mat[i][j] == 1) {
        g.ind[j]--;
        if (g.ind[j] == 0 && comp[j] == u) q.push(j);
      }
    g.ind[i] = -1;
  }
  u++;
  if (u == 4) u = 1;
  if (done == n) return 0;
  return 1 + f(u, g);
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> comp[i];
  graph g;
  for (long long i = 1; i <= n; i++) {
    long long k, prev;
    cin >> k;
    for (long long j = 0; j < k; j++) {
      cin >> prev;
      g.mat[prev][i] = 1;
      g.ind[i]++;
    }
  }
  graph g2 = g;
  graph g3 = g;
  long long aa, bb, cc;
  long long mn = 1000000000;
  done = 0;
  mn = min(mn, aa = f(1, g));
  done = 0;
  mn = min(mn, bb = f(2, g2));
  done = 0;
  mn = min(mn, cc = f(3, g3));
  cout << mn + n << endl;
  return 0;
}
