#include <bits/stdc++.h>
using namespace std;
const int MAXN = 222, MAXLONG = 0x3f3f3f3f;
int n, a[MAXN], pre[MAXN][MAXN], ans;
bool vis[MAXN];
inline bool checkp(const int &x) {
  for (int i = 1; i <= pre[x][0]; i++)
    if (!vis[pre[x][i]]) return false;
  return true;
}
inline bool checka() {
  for (int i = 1; i <= n; i++)
    if (!vis[i]) return false;
  return true;
}
inline void solve(const int &x, int cost) {
  while (true) {
    bool haveans = false;
    for (int i = 1; i <= n; i++)
      if (!vis[i] && checkp(i) && a[i] == x) vis[i] = haveans = true, cost++;
    if (!haveans) break;
  }
  if (checka()) {
    if (ans > cost) ans = cost;
    return;
  }
  solve((x % 3) + 1, cost + 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    int tmp;
    cin >> tmp;
    pre[i][0] = tmp;
    for (int j = 1; j <= tmp; j++) cin >> pre[i][j];
  }
  ans = MAXLONG;
  for (int i = 1; i <= 3; i++) {
    memset(vis, false, sizeof(vis));
    vis[0] = true;
    solve(i, 0);
  }
  cout << ans << endl;
  return 0;
}
