#include <bits/stdc++.h>
using namespace std;
bool can_stage(const vector<int>& dep, const vector<int>& ready) {
  bool can = true;
  for (int i = 0; i < dep.size(); i++) {
    if (!ready[dep[i]]) {
      can = false;
      break;
    }
  }
  return can;
}
int main() {
  int n;
  cin >> n;
  vector<int> comps(n);
  for (int i = 0; i < n; i++) {
    cin >> comps[i];
  }
  vector<vector<int> > c(3, vector<int>());
  for (int i = 0; i < n; i++) {
    c[comps[i] - 1].push_back(i);
  }
  vector<vector<int> > dep(n);
  for (int i = 0; i < n; i++) {
    int s;
    cin >> s;
    vector<int> v(s);
    for (int j = 0; j < s; j++) {
      int t;
      cin >> t;
      v[j] = t - 1;
    }
    dep[i] = v;
  }
  vector<int> no_dep(3, 0);
  for (int i = 0; i < n; i++) {
    if (dep[i].size() == 0) {
      no_dep[comps[i] - 1] = 1;
    }
  }
  int min_time = 1e7;
  int min_comp;
  for (int init_comp = 0; init_comp < 3; init_comp++) {
    if (no_dep[init_comp]) {
      int time = 0;
      vector<int> ready(n, 0);
      int stage;
      for (int i = 0; i < c[init_comp].size(); i++) {
        if (dep[c[init_comp][i]].size() == 0) {
          stage = c[init_comp][i];
        }
      }
      int comp = init_comp;
      while (find(ready.begin(), ready.end(), 0) != ready.end()) {
        ready[stage] = 1;
        time++;
        int i = 0;
        while (i < c[comp].size()) {
          if (ready[c[comp][i]] == 0 && can_stage(dep[c[comp][i]], ready)) {
            ready[c[comp][i]] = 1;
            time++;
            i = 0;
          } else {
            i++;
          }
        }
        comp = (comp + 1) % 3;
      }
      if (time < min_time) {
        min_time = time;
        min_comp = comp;
      }
    }
  }
  cout << min_time << endl;
  return 0;
}
