#include <bits/stdc++.h>
using namespace std;
vector<int> ready[3];
vector<int> temp[3];
int done[203], n, part[203], cnt[3];
int init[203];
vector<int> store[203];
int total = 0;
void Print() {
  for (int i = 0; i < 3; i++) {
    cout << "printing ready queue for " << i << endl;
    for (int j = cnt[i]; j < ready[i].size(); j++) {
      cout << ready[i][j] << " ";
    }
    cout << endl;
  }
  for (int i = 0; i < n; i++) {
    cout << i << " " << done[i] << " " << init[i] << endl;
  }
}
int doit(int no) {
  int ans = 0;
  for (int v = cnt[no]; v < ready[no].size(); v++) {
    ans++, total++;
    int d = ready[no][v];
    for (int i = 0; i < store[d].size(); i++) {
      done[store[d][i]]++;
      if (done[store[d][i]] == init[store[d][i]]) {
        ready[part[store[d][i]]].push_back(store[d][i]);
      }
    }
  }
  cnt[no] = ready[no].size();
  if (total == n) return ans;
  ans++;
  return ans + doit((no + 1) % 3);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> part[i];
    part[i]--;
  }
  int a, ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    cin >> init[i];
    if (init[i] == 0) {
      ready[part[i]].push_back(i);
    }
    for (int j = 0; j < init[i]; j++) {
      scanf("%d", &a);
      a--;
      store[a].push_back(i);
    }
  }
  temp[0] = ready[0], temp[1] = ready[1], temp[2] = ready[2];
  for (int i = 0; i < 3; i++) {
    memset(done, 0, sizeof done);
    cnt[0] = cnt[1] = cnt[2] = total = 0;
    ans = min(ans, doit(i));
    ready[0] = temp[0], ready[1] = temp[1], ready[2] = temp[2];
  }
  cout << ans << endl;
  return 0;
}
