#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int oo = 210000000;
int n;
vector<int> p[4];
vector<int> g[201];
int solve(int x) {
  bool used[201];
  for (int i = 1; i <= n; i++) used[i] = 0;
  int ans = 0, m = 0;
  while (m < n) {
    bool flag1 = 0;
    for (int i = 0; i < p[x].size(); i++) {
      int cur = p[x][i];
      if (used[cur]) continue;
      bool flag = 1;
      for (int j = 0; j < g[cur].size(); j++)
        if (!used[g[cur][j]]) {
          flag = 0;
          break;
        }
      if (flag) {
        flag1 = 1;
        m++;
        used[cur] = 1;
      }
    }
    if (!flag1) {
      x++;
      if (x > 3) x = 1;
      ans++;
    }
  }
  return ans;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int tmp;
    cin >> tmp;
    p[tmp].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int tmp1;
      cin >> tmp1;
      g[i].push_back(tmp1);
    }
  }
  int ans = oo;
  ans = min(ans, solve(1));
  ans = min(ans, solve(2));
  ans = min(ans, solve(3));
  cout << ans + n << endl;
  return (0);
}
