#include <bits/stdc++.h>
using namespace std;
struct node {
  int v;
  node *n;
};
int n, *C;
node **G;
int *deg;
bool *sursa;
int mi = 1000000;
void citire() {
  cin >> n;
  C = new int[n + 1];
  for (int i = 1; i <= n; ++i) {
    cin >> C[i];
  }
  G = new node *[n + 1]();
  deg = new int[n + 1]();
  for (int i = 1; i <= n; ++i) {
    cin >> deg[i];
    for (int j = 1; j <= deg[i]; ++j) {
      int x;
      cin >> x;
      node *edge = new node;
      edge->v = i;
      edge->n = G[x];
      G[x] = edge;
    }
  }
}
void calc(int ch) {
  queue<int> *q = new queue<int>[3]();
  int *deg_cpy = new int[n + 1]();
  for (int i = 1; i <= n; ++i) {
    if (deg[i] == 0) {
      q[C[i] - 1].push(i);
    }
    deg_cpy[i] = deg[i];
  }
  int cost = 0;
  while (q[0].size() + q[1].size() + q[2].size() > 0) {
    while (q[ch - 1].size() > 0) {
      int x = q[ch - 1].front();
      for (node *it = G[x]; it; it = it->n) {
        --deg_cpy[it->v];
        if (deg_cpy[it->v] == 0) {
          q[C[it->v] - 1].push(it->v);
        }
      }
      q[ch - 1].pop();
    }
    ch = ch % 3 + 1;
    if (q[0].size() + q[1].size() + q[2].size() > 0) {
      ++cost;
    }
  }
  if (mi > cost) {
    mi = cost;
  }
  delete deg_cpy;
}
void afisare() {
  calc(1);
  calc(2);
  calc(3);
  cout << mi + n << "\n";
}
int main() {
  citire();
  afisare();
}
