#include <bits/stdc++.h>
using namespace std;
vector<int> edge[205];
int n, a[205], k, u, v, in[205], tmp[205], cur;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> k;
    while (k--) {
      cin >> u;
      u--;
      edge[u].push_back(i);
      in[i]++;
    }
  }
  int ans = 2e9;
  for (int ii = 1; ii <= 3; ii++) {
    for (int i = 0; i < n; i++) tmp[i] = in[i];
    int nn = 0, cnt = 0;
    cur = ii;
    while (nn < n) {
      bool lesa = 1;
      while (lesa) {
        lesa = 0;
        for (int i = 0; i < n; i++) {
          if (a[i] == cur && !tmp[i]) {
            nn++;
            tmp[i]--;
            for (auto j : edge[i]) tmp[j]--;
            lesa = 1;
          }
        }
      }
      cur++;
      cnt++;
      if (cur == 4) cur = 1;
      if (nn >= n) cnt--;
    }
    ans = min(ans, cnt);
  }
  cout << ans + n;
  return 0;
}
