#include <bits/stdc++.h>
using namespace std;
int c[222], dep[222][222], was[222], k[222], order[222], on = 0, n, dc[222];
int Solve(int x) {
  queue<int> nodep[4];
  for (int i = 1; i <= n; i++) {
    dc[i] = k[i];
    if (k[i] == 0) {
      nodep[c[i]].push(i);
    }
  }
  int pro = 0, ans = 0;
  while (pro < n) {
    while (!nodep[x].empty()) {
      int y = nodep[x].front();
      nodep[x].pop();
      ++pro;
      ++ans;
      for (int i = 1; i <= n; i++)
        if (dep[i][y]) {
          --dc[i];
          if (dc[i] == 0) {
            nodep[c[i]].push(i);
          }
        }
    }
    if (pro == n) return ans;
    x = x % 3 + 1;
    ++ans;
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> c[i];
  for (int i = 1; i <= n; ++i) {
    cin >> k[i];
    for (int j = 0; j < k[i]; ++j) {
      int x;
      cin >> x;
      dep[i][x] = 1;
    }
  }
  cout << std::min(std::min(Solve(1), Solve(2)), Solve(3));
  return 0;
}
