#include <bits/stdc++.h>
using namespace std;
long long MOD_EXPO(long long b, long long p, long long m) {
  if (p == 0) return 1;
  long long ret = MOD_EXPO(b, p / 2, m) % m;
  ret = (ret * ret) % m;
  return ((p & 1) ? (ret * b) % m : ret % m);
}
long long POWER(long long N, long long K) {
  long long i, ans = 1;
  for (i = 1; i <= K; i++) ans *= N;
  return ans;
}
int SET(int N, int pos) { return (N | (1 << pos)); }
int RESET(int N, int pos) { return (N & !(1 << pos)); }
bool CHECK(int N, int pos) { return (N & (1 << pos)); }
int dx4[] = {1, -1, 0, 0};
int dy4[] = {0, 0, 1, -1};
int dx6[] = {0, 0, 1, -1, 0, 0};
int dy6[] = {1, -1, 0, 0, 0, 0};
int dz6[] = {0, 0, 0, 0, 1, -1};
int dx8[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dy8[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dkx8[] = {-1, 1, -1, 1, -2, -2, 2, 2};
int dky8[] = {2, 2, -2, -2, 1, -1, 1, -1};
int tc = 1;
const double eps = 1e-9;
const double pi = acos(-1.0);
const long long int mx = 200;
const long long int mod = 1e9 + 7;
bool check[mx + 5][mx + 5];
int comp[mx + 5];
bool done[mx + 5];
int main() {
  int i, j, k, n, cnt, a, device, ans, mini_ans;
  bool flag, next;
  cin >> n;
  for (i = 1; i <= n; i++) scanf("%d", &comp[i]);
  cnt = 0;
  device = 3;
  for (i = 1; i <= n; i++) {
    scanf("%d", &k);
    for (j = 1; j <= k; j++) {
      scanf("%d", &a);
      check[i][a] = true;
    }
  }
  mini_ans = INT_MAX;
  for (k = 1; k <= 3; k++) {
    cnt = 0;
    ans = 0;
    device = k;
    memset(done, 0, sizeof(done));
    while (cnt < n) {
      next = true;
      for (i = 1; i <= n && cnt < n; i++) {
        if (comp[i] == device && !done[i]) {
          flag = true;
          for (j = 1; j <= n; j++) {
            if (check[i][j] && !done[j]) {
              flag = false;
              break;
            }
          }
          if (flag) {
            done[i] = true;
            next = false;
            cnt++;
            ans++;
            break;
          }
        }
      }
      if (next) device++, ans++;
      if (device > 3) device = 1;
    }
    mini_ans = min(mini_ans, ans);
  }
  cout << mini_ans << "\n";
  return 0;
}
