#include <bits/stdc++.h>
using namespace std;
vector<int> g[210];
int n, k, o, c[210];
bool v[210];
int dfs(int now) {
  for (;;) {
    bool change = false;
    for (int i = 0; i < n; ++i)
      if (!v[i] && c[i] == now) {
        bool ok = true;
        for (int j = 0; j < g[i].size(); ++j) {
          int p = g[i][j];
          if (!v[p]) ok = false;
        }
        if (ok) {
          v[i] = true;
          change = true;
        }
      }
    if (!change) break;
  }
  if (count(v, v + n, true) == n) return 0;
  return 1 + dfs((now + 1) % 3);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &c[i]), c[i]--;
  for (int i = 0; i < n; ++i) {
    g[i].clear();
    scanf("%d", &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d", &o), --o;
      g[i].push_back(o);
    }
  }
  int ans = ~0U >> 2;
  for (int i = 0; i < 3; ++i) {
    memset(v, 0, sizeof v);
    ans = min(ans, dfs(i) + n);
  }
  cout << ans << endl;
  return 0;
}
