#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200 * 1000 + 20;
vector<int> adj[MAX_N];
vector<int> a[MAX_N];
vector<int> T_S;
bool node[MAX_N];
bool mark[MAX_N];
int P[MAX_N];
int n, ans, Size;
void Topology_Sort(int v) {
  node[v] = 1;
  for (int i = 0; i < adj[v].size(); i++) {
    int u = adj[v][i];
    if (!node[u]) Topology_Sort(u);
  }
  T_S.push_back(v);
}
void Computer(int place) {
  int sum = 0;
  for (int i = Size - 1; i >= 0; i--) {
    int v = T_S[i];
    if (!mark[v] && P[v] == place) {
      bool flag = 1;
      for (int j = 0; j < a[v].size(); j++) {
        int u = a[v][j];
        if (!mark[u]) flag = 0;
      }
      if (flag) {
        sum++;
        mark[v] = 1;
      }
    } else if (mark[v])
      sum++;
  }
  if (sum != n) {
    ans++;
    place++;
    place %= 3;
    if (!place) place = 3;
    return Computer(place);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> P[i];
  for (int v = 0; v < n; v++) {
    int deg;
    cin >> deg;
    while (deg > 0) {
      int u;
      cin >> u;
      --u;
      adj[u].push_back(v);
      a[v].push_back(u);
      deg--;
    }
  }
  for (int i = 0; i < n; i++)
    if (!node[i]) Topology_Sort(i);
  Size = T_S.size();
  ans = 0;
  int answer = 0;
  Computer(1);
  answer = ans;
  ans = 0;
  memset(mark, 0, sizeof mark);
  Computer(2);
  answer = min(ans, answer);
  ans = 0;
  memset(mark, 0, sizeof mark);
  Computer(3);
  answer = min(ans, answer);
  cout << answer + n << endl;
}
