#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 + 10, inf = 1e9;
vector<int> out[maxn], in[maxn];
int a[maxn], cnt[maxn], mark[maxn], ord[maxn];
vector<int> ts;
void dfs(int v) {
  if (mark[v]) return;
  mark[v] = 1;
  for (int u : out[v]) dfs(u);
  ts.push_back(v);
}
bool cmp(int i, int j) { return ord[i] < ord[j]; }
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int u;
      cin >> u;
      u--;
      out[u].push_back(i);
      in[i].push_back(u);
    }
  }
  for (int i = 0; i < n; i++) dfs(i);
  reverse(ts.begin(), ts.end());
  for (int i = 0; i < n; i++) ord[ts[i]] = i;
  int ans = inf;
  for (int st = 0; st < 3; st++) {
    int cur = st;
    for (int i = 0; i < n; i++) cnt[i] = in[i].size();
    memset(mark, 0, sizeof mark);
    int Rem = n;
    int haz = 0;
    while (1) {
      vector<int> v;
      for (int i = 0; i < n; i++) {
        if (!mark[i] && a[i] == cur) v.push_back(i);
      }
      sort(v.begin(), v.end(), cmp);
      if (!v.size()) {
        haz++;
        cur = (cur + 1) % 3;
        continue;
      }
      for (int i : v) {
        if (!cnt[i]) {
          mark[i] = 1;
          Rem--;
          for (int j : out[i]) cnt[j]--;
        }
      }
      cur = (cur + 1) % 3;
      if (!Rem) break;
      haz++;
    }
    ans = min(ans, haz + n);
  }
  cout << ans << '\n';
}
