#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 + 55;
int n;
int color[maxn];
int sz[maxn];
int adj[maxn][maxn];
vector<int> rg[maxn];
int deg[maxn];
bool visi[maxn];
int filter(int cc) {
  queue<int> q;
  for (int i = 0; i < n; ++i) {
    if (visi[i]) continue;
    if (deg[i]) continue;
    if (color[i] != cc) continue;
    q.push(i);
    visi[i] = 1;
  }
  int ret = 0;
  while (!q.empty()) {
    ret++;
    int u = q.front();
    q.pop();
    for (int i = 0; i < rg[u].size(); ++i) {
      int nxt = rg[u][i];
      deg[nxt]--;
      if (deg[nxt] == 0 && color[nxt] == cc && visi[nxt] == false) {
        visi[nxt] = 1;
        q.push(nxt);
      }
    }
  }
  return ret;
}
int calc(int start) {
  memset(visi, false, sizeof(visi));
  memset(deg, 0, sizeof(deg));
  for (int i = 0; i < n; ++i) deg[i] = sz[i];
  int ret = -1;
  int curr = start;
  int rem = n;
  while (rem > 0) {
    int t = filter(curr);
    rem -= t;
    ret++;
    curr = (curr + 1) % 3;
  }
  return ret + n;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> color[i];
    color[i]--;
  }
  for (int i = 0; i < n; ++i) {
    cin >> sz[i];
    for (int j = 0; j < sz[i]; ++j) {
      int v;
      cin >> v;
      v--;
      adj[i][j] = v;
      rg[v].push_back(i);
    }
  }
  int sol = (int)1e9;
  for (int start = 0; start <= 2; ++start) {
    int tmp = calc(start);
    sol = min(sol, tmp);
  }
  cout << sol << endl;
  return 0;
}
