#include <bits/stdc++.h>
using namespace std;
const int maxn = 205;
const int inf = 0x3fffffff;
int dp[maxn][4], e, head[maxn], vis[maxn], A[maxn], G[4][4], ans, in[maxn],
    in1[maxn], sum;
struct {
  int t, next, w;
} edge[maxn * maxn];
void add(int a, int b) {
  edge[e].t = b;
  edge[e].next = head[a];
  head[a] = e++;
}
void dfs(int u) {
  int v;
  sum--;
  vis[u] = true;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    v = edge[i].t;
    in1[v]--;
    if (A[u] == A[v] && in1[v] == 0 && !vis[v]) dfs(v);
  }
}
int main() {
  int n, k, x, src;
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &A[i]);
      A[i]--;
    }
    src = 1;
    memset(head, -1, sizeof(head));
    memset(in, 0, sizeof(in));
    e = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &k);
      in[i] = k;
      while (k--) {
        scanf("%d", &x);
        add(x, i);
      }
    }
    ans = inf;
    int pre = 0, t, k, cnt;
    bool f;
    memset(vis, false, sizeof(vis));
    for (int z = 0; z <= 2; z++) {
      memcpy(in1, in, sizeof(in));
      memset(vis, false, sizeof(vis));
      sum = n;
      k = z;
      cnt = 0;
      while (1) {
        for (int i = 1; i <= n; i++) {
          if (in1[i] == 0 && A[i] == k && !vis[i]) {
            in1[i]--;
            dfs(i);
          };
        }
        if (sum > 0) {
          k = (k + 1) % 3;
          cnt++;
        } else
          break;
      }
      ans = min(ans, n + cnt);
    }
    printf("%d\n", ans);
  }
  return 0;
}
