#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
const long long N = 2e2 + 5;
long long hou[N];
long long n;
vector<long long> adj[N];
long long mat[4][4];
vector<long long> deg;
long long solve(long long cur) {
  vector<long long> td = deg;
  queue<long long> qq[4];
  for (long long i = 1; i <= n; i++) {
    if (td[i] == 0) qq[hou[i]].push(i);
  }
  long long ans = -1;
  bool vis[n + 1];
  memset(vis, false, sizeof vis);
  while (!qq[1].empty() || !qq[2].empty() || !qq[3].empty()) {
    ans++;
    while (!qq[cur].empty()) {
      long long u = qq[cur].front();
      qq[cur].pop();
      if (vis[u]) continue;
      vis[u] = true;
      for (long long v : adj[u]) {
        td[v]--;
        if (!td[v]) {
          qq[hou[v]].push(v);
        }
      }
    }
    cur++;
    if (cur == 4) cur = 1;
  }
  return max(ans, 0LL);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  mat[1][2] = 1, mat[1][3] = 2, mat[2][1] = 2, mat[2][3] = 1, mat[3][1] = 1,
  mat[3][2] = 2;
  for (long long i = 1; i <= n; i++) cin >> hou[i];
  long long x, y;
  deg.resize(n + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> x;
    deg[i] = x;
    for (long long j = 0; j < x; j++) {
      cin >> y;
      adj[y].push_back(i);
    }
  }
  long long ans = (long long)1e18 + 5;
  for (long long i = 1; i <= 3; i++) {
    ans = min(ans, solve(i));
  }
  cout << ans + n << "\n";
  return 0;
}
