#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 202;
const int oo = 1e6;
int c[MAX_N];
vector<int> p[MAX_N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> c[i], --c[i];
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    while (k--) {
      int x;
      cin >> x;
      --x;
      p[x].push_back(i);
    }
  }
  int ans = oo;
  for (int t = 0; t < 3; t++) {
    int f[MAX_N] = {0}, res = -1;
    queue<int> q[3];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < p[i].size(); j++) f[p[i][j]]++;
    for (int i = 0; i < n; i++)
      if (!f[i]) q[c[i]].push(i), f[i] = oo;
    for (int i = t; q[0].size() + q[1].size() + q[2].size(); ++res, ++i)
      for (int j = i % 3; q[j].size(); q[j].pop()) {
        int x = q[j].front();
        for (int k = 0; k < p[x].size(); k++) --f[p[x][k]];
        for (int k = 0; k < n; k++)
          if (!f[k]) q[c[k]].push(k), f[k] = oo;
      }
    ans = min(ans, res + n);
  }
  cout << ans << endl;
  return 0;
}
