#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double EPS = 1e-9;
int mods(int a, int b) { return (b + (a % b)) % b; }
int n, k, a, cur_time, ans = INF, memo_indegree[205], indegree[205],
                       computers[205];
vector<int> sources;
vector<vector<int> > adjList, pos;
void recover() {
  for (int i = 0; i <= 200; i++) indegree[i] = memo_indegree[i];
  for (int i = 0; i < sources.size(); i++)
    pos[computers[sources[i]]].push_back(sources[i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  adjList.resize(n);
  pos.resize(3);
  for (int i = 0; i < n; i++) {
    cin >> computers[i];
    computers[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> k;
    if (!k) sources.push_back(i);
    for (int j = 0; j < k; j++) {
      cin >> a;
      adjList[a - 1].push_back(i);
      memo_indegree[i]++;
    }
  }
  for (int start = 0; start < 3; start++) {
    recover();
    cur_time = 0;
    int cur = start;
    while (!pos[0].empty() || !pos[1].empty() || !pos[2].empty()) {
      for (int i = 0; i < 3; i++) {
        if (!pos[(cur + i) % 3].empty()) {
          cur_time += i + 1;
          int u = pos[(cur + i) % 3].back();
          pos[(cur + i) % 3].pop_back();
          for (int j = 0; j < adjList[u].size(); j++) {
            int v = adjList[u][j];
            if (indegree[v]) {
              indegree[v]--;
              if (!indegree[v]) pos[computers[v]].push_back(v);
            }
          }
          cur = (cur + i) % 3;
          break;
        }
      }
    }
    ans = min(ans, cur_time);
  }
  cout << ans << '\n';
  return 0;
}
