#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> vv[201];
int computer[201], in[201], bk[201], temp[201];
int bfs(int st) {
  queue<int> q;
  int ans = n;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0) q.push(i);
  }
  while (!q.empty()) {
    memset(bk, 0, sizeof(bk));
    while (!q.empty()) {
      int head = q.front();
      q.pop();
      if (bk[head] == 5) {
        q.push(head);
        break;
      }
      bk[head]++;
      if (computer[head] != st) {
        q.push(head);
        continue;
      }
      for (int i = 0; i < vv[head].size(); i++) {
        int v = vv[head][i];
        in[v]--;
        if (in[v] == 0) q.push(v);
      }
    }
    st = (st == 3 ? 1 : st + 1);
    if (!q.empty()) ans++;
  }
  return ans;
}
int main() {
  while (cin >> n) {
    memset(in, 0, sizeof(in));
    for (int i = 1; i <= n; i++) vv[i].clear();
    for (int i = 1; i <= n; i++) cin >> computer[i];
    for (int i = 1; i <= n; i++) {
      int ki;
      cin >> ki;
      for (int j = 1; j <= ki; j++) {
        int aj;
        cin >> aj;
        vv[aj].push_back(i);
        in[i]++;
      }
    }
    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= n; i++) temp[i] = in[i];
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= n; j++) in[j] = temp[j];
      ans = min(ans, bfs(i));
    }
    cout << ans << endl;
  }
  return 0;
}
