#include <bits/stdc++.h>
using namespace std;
long long n, ans = 1000000000;
vector<long long> b, comp(201, 0);
vector<vector<long long> > a(201, b);
void f(vector<bool> used, long long already, long long last) {
  long long i, j;
  bool flag = false;
  for (i = 1; i <= n; i++)
    if (!used[i]) flag = true;
  if (!flag) {
    ans = min(ans, already);
    return;
  }
  bool atleastone = false;
  bool can = true;
  long long num;
  while (can) {
    can = false;
    for (i = 1; i <= n; i++) {
      if (used[i])
        continue;
      else if (comp[i] != last)
        continue;
      else {
        bool ff = true;
        for (j = 0; j < a[i].size(); j++) {
          num = a[i][j];
          if (!used[num]) ff = false;
        }
        if (ff) {
          atleastone = true;
          used[i] = true;
          can = true;
        }
      }
    }
  }
  flag = false;
  for (i = 1; i <= n; i++)
    if (!used[i]) flag = true;
  if (!flag) {
    ans = min(ans, already);
    return;
  }
  if (last == 1) {
    f(used, already + 1, 2);
  } else if (last == 2) {
    f(used, already + 1, 3);
  } else {
    f(used, already + 1, 1);
  }
}
int main() {
  cin >> n;
  long long i, j;
  for (i = 1; i <= n; i++) cin >> comp[i];
  for (i = 1; i <= n; i++) {
    long long t;
    cin >> t;
    for (j = 0; j < t; j++) {
      long long q;
      cin >> q;
      a[i].push_back(q);
    }
    sort(a[i].begin(), a[i].end());
  }
  vector<bool> q;
  q.push_back(false);
  for (i = 1; i <= n; i++) q.push_back(false);
  f(q, 0, 1);
  f(q, 0, 2);
  f(q, 0, 3);
  cout << ans + n;
  return 0;
}
