#include <bits/stdc++.h>
using namespace std;
int n, ans = INT_MAX;
int c[205], in[205], tin[205];
vector<int> mp[205];
int bfs(int x) {
  queue<int> q[4];
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) q[c[i]].push(i);
  for (int i = 1; i <= n; i++) tin[i] = in[i];
  int res = -1;
  while (!q[1].empty() || !q[2].empty() || !q[3].empty()) {
    res++;
    while (!q[x].empty()) {
      int u = q[x].front();
      q[x].pop();
      res++;
      for (int i = 0; i < mp[u].size(); i++) {
        int v = mp[u][i];
        tin[v]--;
        if (!tin[v]) q[c[v]].push(v);
      }
    }
    if (x == 1)
      x = 2;
    else if (x == 2)
      x = 3;
    else if (x == 3)
      x = 1;
  }
  if (res == -1) return 0;
  return res;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      mp[x].push_back(i);
      in[i]++;
    }
  }
  for (int i = 1; i <= 3; i++) ans = min(ans, bfs(i));
  cout << ans << endl;
}
