#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int n;
map<int, set<int> > room;
vector<int> G[205];
queue<int> start[4];
queue<int> startc[4];
int in[205], c[205];
bool allEmpty() {
  for (int i = 1; i <= 3; ++i) {
    if (!startc[i].empty()) return 0;
  }
  return 1;
}
int solve(int now) {
  int inc[205];
  for (int i = 1; i <= n; ++i) inc[i] = in[i];
  for (int k = 1; k <= 3; ++k) startc[k] = start[k];
  int tim = 0;
  while (!allEmpty()) {
    while (!startc[now].empty()) {
      int u = startc[now].front();
      startc[now].pop();
      tim++;
      for (int v : G[u]) {
        if (--inc[v] == 0) {
          startc[c[v]].push(v);
        }
      }
    }
    now = now % 3 + 1;
    tim++;
  }
  return tim - 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
    room[c[i]].insert(i);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> in[i];
    if (!in[i]) start[c[i]].push(i);
    for (int j = 0, v; j < in[i]; ++j) {
      cin >> v;
      G[v].push_back(i);
    }
  }
  int ans = 2e9;
  for (int k = 1; k <= 3; ++k) {
    ans = min(ans, solve(k));
  }
  cout << ans << endl;
}
