#include <bits/stdc++.h>
int next[203][203];
int degree[203], value[203];
int queue[3][203], front[3], rear[3];
int have[203];
int main() {
  int n, i, j, k, total, tmp, ans = 100000000, qans, now;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", value + i);
  for (i = 1; i <= n; i++) {
    scanf("%d", &total);
    degree[i] = total;
    for (j = 0; j < total; j++) {
      scanf("%d", &tmp);
      next[tmp][++next[tmp][0]] = i;
    }
  }
  for (i = 0; i < 3; i++) {
    memset(have, 0, sizeof(have));
    memset(front, 0, sizeof(front));
    memset(rear, 0, sizeof(rear));
    for (j = 1; j <= n; j++)
      if (have[j] == degree[j]) queue[value[j] - 1][rear[value[j] - 1]++] = j;
    qans = 0;
    now = i;
    while (1) {
      for (; front[now] < rear[now]; front[now]++) {
        qans++;
        int temp = queue[now][front[now]];
        for (int xx = 1; xx <= next[temp][0]; xx++) {
          have[next[temp][xx]]++;
          if (have[next[temp][xx]] == degree[next[temp][xx]])
            queue[value[next[temp][xx]] - 1]
                 [rear[value[next[temp][xx]] - 1]++] = next[temp][xx];
        }
      }
      for (k = 0; k < 3; k++)
        if (front[k] < rear[k]) break;
      if (k > 2) break;
      qans++;
      now = (now + 1) % 3;
    }
    ans = ((qans) < (ans) ? (qans) : (ans));
  }
  printf("%d\n", ans);
  return 0;
}
