#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long inf = 1ll << 61;
int n, num, k, don;
vector<int> execute[4];
vector<bool> done(201);
vector<int> comp(201);
vector<int> req[201];
vector<int> start;
bool func(int iter) {
  for (int i = 0; i < execute[iter].size(); i++) {
    int con = execute[iter][i];
    if (!done[con]) {
      bool flag = true;
      for (int j = 0; j < req[con].size(); j++) {
        if (!done[req[con][j]]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        done[con] = true;
        don++;
        return true;
      }
    }
  }
  return false;
}
int solve(int iter) {
  int temp = -1;
  don = 0;
  for (int i = 0; i < 201; i++) {
    done[i] = false;
  }
  while (true) {
    temp++;
    bool what = true;
    while (what) {
      what = func(iter);
      if (what) {
        temp++;
      }
    }
    iter++;
    if (iter == 4) {
      iter = 1;
    }
    if (don == n) {
      break;
    }
  }
  return temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> num;
    execute[num].push_back(i);
    comp[i] = num;
  }
  for (int i = 1; i <= n; i++) {
    cin >> k;
    if (!k) {
      start.push_back(comp[i]);
    }
    for (int j = 0; j < k; j++) {
      cin >> num;
      req[i].push_back(num);
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < start.size(); i++) {
    int temp = solve(start[i]);
    ans = min(ans, temp);
  }
  cout << ans;
}
