#include <bits/stdc++.h>
using namespace std;
int n, ans = (int)1e9;
set<int> q[3][300];
vector<int> w[3];
int id[300], start, cnt, cur = 0;
bool used[300];
int solve() {
  int ans = 0;
  while (cnt) {
    for (int i = 0; i < w[start].size(); i++) {
      int to = w[start][i];
      if (!used[to] && q[cur][to].size() == 0) {
        ans++;
        cnt--;
        for (int j = 1; j <= n; j++) {
          q[cur][j].erase(to);
        }
        used[to] = true;
        i = -1;
      }
    }
    if (cnt) {
      start++;
      start %= 3;
      ans++;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> id[i];
    w[id[i] - 1].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int y;
    cin >> y;
    for (int j = 1; j <= y; j++) {
      int tmp;
      cin >> tmp;
      q[0][i].insert(tmp);
      q[1][i].insert(tmp);
      q[2][i].insert(tmp);
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 1; j <= n; j++) {
      used[j] = false;
    }
    cur = i;
    cnt = n;
    start = i;
    ans = min(ans, solve());
  }
  cout << ans;
  return 0;
}
