#include <bits/stdc++.h>
using namespace std;
const int N = 1234;
int n;
int a[N], in[4][N];
vector<vector<int> > graph;
queue<int> qq[4][4];
int solve(int r) {
  int curr = r, visited = 0, ans = 0;
  int node = -1;
  while (true) {
    while (!qq[r][curr].empty()) {
      node = qq[r][curr].front();
      qq[r][curr].pop();
      visited++;
      ans++;
      for (int i = 0; i < (int)graph[node].size(); i++) {
        int child = graph[node][i];
        in[r][child]--;
        if (in[r][child] == 0) qq[r][a[child] - 1].push(child);
      }
    }
    if (visited == n) break;
    curr++;
    curr %= 3;
    ans++;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  graph.resize(n);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      x--;
      graph[x].push_back(i);
      in[0][i]++;
      in[1][i]++;
      in[2][i]++;
    }
  }
  int root = -1;
  for (int i = 0; i < n; i++) {
    if (in[0][i] == 0) {
      root = i;
      qq[0][a[root] - 1].push(root);
      qq[1][a[root] - 1].push(root);
      qq[2][a[root] - 1].push(root);
    }
  }
  assert(root != -1);
  int ans = solve(0);
  ans = min(ans, solve(1));
  ans = min(ans, solve(2));
  cout << ans << "\n";
  return 0;
}
