#include <bits/stdc++.h>
using namespace std;
inline void OPEN(string a, bool out = false) {
  freopen(string(a + ".in").c_str(), "r", stdin);
  if (out) freopen(string(a + ".out").c_str(), "w", stdout);
}
vector<int> daftar[5], rek[205];
int proses[205], sum[205];
bool sudah[205];
int n;
int coba(int k) {
  if (!n) return 0;
  ;
  int x = (int)daftar[k].size();
  int ret = 0;
  for (int(i) = (0), _n = (x); (i) < _n; (i)++)
    if (!sudah[daftar[k][i]] && !proses[daftar[k][i]]) {
      --n;
      ++ret;
      int v = daftar[k][i];
      sudah[v] = 1;
      for (int(i) = (0), _n = ((int)rek[v].size()); (i) < _n; (i)++)
        --proses[rek[v][i]];
    }
  if (ret)
    return coba(k) + ret;
  else
    return 1 + coba(k % 3 + 1);
}
int main() {
  n = ({
    int __x_;
    scanf("%d", &__x_);
    __x_;
  });
  int m = n;
  for (int(i) = (1), _n = (n); (i) <= _n; (i)++) {
    int x = ({
      int __x_;
      scanf("%d", &__x_);
      __x_;
    });
    daftar[x].push_back(i);
  }
  for (int(i) = (1), _n = (n); (i) <= _n; (i)++) {
    sum[i] = ({
      int __x_;
      scanf("%d", &__x_);
      __x_;
    });
    for (int(j) = (0), _n = (sum[i]); (j) < _n; (j)++) {
      int x = ({
        int __x_;
        scanf("%d", &__x_);
        __x_;
      });
      rek[x].push_back(i);
    }
  }
  int ans = 1000111000;
  for (int(k) = (1), _n = (3); (k) <= _n; (k)++) {
    ;
    n = m;
    for (int(i) = (1), _n = (n); (i) <= _n; (i)++) proses[i] = sum[i];
    memset(sudah, 0, sizeof sudah);
    ans = min(ans, coba(k));
  }
  printf("%d\n", ans);
}
