#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 205;
int n;
int c[MAX_N];
int deg[MAX_N];
vector<int> r[MAX_N];
vector<int> g[MAX_N];
vector<int> q[4];
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &c[i]);
    --c[i];
  }
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    for (int j = 0; j < x; ++j) {
      int y;
      scanf("%d", &y);
      g[i].push_back(y - 1);
      r[y - 1].push_back(i);
    }
  }
  int res = 987654321;
  for (int sta_comp = 0; sta_comp <= 2; ++sta_comp) {
    memset(deg, 0, sizeof(deg));
    for (int i = 0; i < n; ++i) deg[i] = g[i].size();
    for (int i = 0; i < 3; ++i) q[i].clear();
    for (int i = 0; i < n; ++i)
      if (deg[i] == 0) q[c[i]].push_back(i);
    int cur_comp = sta_comp;
    int cur_res = 0;
    while (1) {
      bool end = true;
      for (int i = 0; i < 3; ++i)
        if (q[i].size() != 0) end = false;
      if (end) break;
      if (q[cur_comp].size() == 0) {
        ++cur_res;
        cur_comp = (cur_comp + 1) % 3;
      } else {
        ++cur_res;
        int node = q[cur_comp][q[cur_comp].size() - 1];
        q[cur_comp].pop_back();
        for (int i = 0; i < r[node].size(); ++i) {
          int nnode = r[node][i];
          if (--deg[nnode] == 0) q[c[nnode]].push_back(nnode);
        }
      }
    }
    res = min(res, cur_res);
  }
  cout << res << endl;
  return 0;
}
