#include <bits/stdc++.h>
using namespace std;
int n, ans, cnt[205], c[205], tmp[205], fa[205];
vector<int> con[205];
queue<int> q;
bool vis[205];
void init() {
  int x, y;
  memset(cnt, 0, sizeof(cnt));
  memset(fa, -1, sizeof(fa));
  for (int i = 1; i <= n; i++) {
    con[i].clear();
    scanf("%d", &c[i]);
    c[i]--;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    while (x--) {
      scanf("%d", &y);
      con[y].push_back(i);
      cnt[i]++;
    }
  }
}
int test(int cur) {
  int remain = n, re = 0, x;
  memset(vis, false, sizeof(vis));
  memcpy(tmp, cnt, sizeof(cnt));
  while (remain > 0) {
    for (int i = 1; i <= n; i++)
      if (!vis[i] && !tmp[i] && c[i] == cur) {
        vis[i] = true;
        q.push(i);
        while (!q.empty()) {
          x = q.front();
          q.pop();
          remain--;
          vis[x] = true;
          for (int j = 0; j <= (int)con[x].size() - 1; j++)
            if (!vis[con[x][j]]) {
              tmp[con[x][j]]--;
              if (!tmp[con[x][j]] && c[con[x][j]] == cur) {
                vis[con[x][j]] = true;
                q.push(con[x][j]);
              }
            }
        }
      }
    if (remain == 0) break;
    cur = (cur + 1) % 3;
    re++;
  }
  return re;
}
int main() {
  while (~scanf("%d", &n)) {
    init();
    ans = 205 * 205;
    for (int i = 0; i <= 2; i++) ans = min(ans, n + test(i));
    printf("%d\n", ans);
  }
  return 0;
}
