#include <bits/stdc++.h>
using namespace std;
vector<int> c, inp;
vector<vector<int> > G, GT;
int n;
int calc(int ini) {
  queue<int> q[3];
  vector<int> cinp = inp;
  int fets = 0, ans = 0;
  for (int i = 0; i < n; ++i)
    if (cinp[i] == 0) q[c[i]].push(i);
  int on = ini;
  while (fets < n) {
    while (!q[on].empty()) {
      int p = q[on].front();
      q[on].pop();
      ++ans;
      ++fets;
      for (int i = 0; i < GT[p].size(); ++i) {
        int r = GT[p][i];
        if ((--cinp[r]) == 0) q[c[r]].push(r);
      }
    }
    on = (on + 1) % 3;
    if (fets < n) ++ans;
  }
  return ans;
}
int main() {
  cin >> n;
  c = inp = vector<int>(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
    --c[i];
  }
  G = GT = vector<vector<int> >(n);
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    while (k--) {
      int x;
      cin >> x;
      --x;
      G[i].push_back(x);
      inp[i]++;
      GT[x].push_back(i);
    }
  }
  cout << min(calc(0), min(calc(1), calc(2))) << endl;
}
