#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007LL) {
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1LL;
  long long _ret = mypow(mul(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = mul(_ret, _a, _mod);
  return _ret;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0LL;
  long long _ret = mymul(add(_a, _a, _mod), _x >> 1, _mod);
  if (_x & 1) _ret = add(_ret, _a, _mod);
  return _ret;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
void build() {}
int n, c[222], ind[222];
vector<int> v[222];
void init() {
  n = getint();
  for (int i = 1; i <= n; i++) c[i] = getint() - 1;
  for (int i = 1; i <= n; i++) {
    ind[i] = getint();
    for (int j = 0; j < ind[i]; j++) {
      int fr = getint();
      v[fr].push_back(i);
    }
  }
}
vector<int> q[3];
int tind[222];
int go(int st, int nxt) {
  for (int i = 0; i < 3; i++) q[i].clear();
  for (int i = 1; i <= n; i++) {
    tind[i] = ind[i];
    if (ind[i] == 0) q[c[i]].push_back(i);
  }
  int gt = 0, ret = 0;
  while (gt < n) {
    while (q[st].size()) {
      int who = q[st].back();
      q[st].pop_back();
      gt++;
      ret++;
      for (auto tmp : v[who]) {
        tind[tmp]--;
        if (tind[tmp] == 0) {
          q[c[tmp]].push_back(tmp);
        }
      }
    }
    if (gt == n) break;
    ret++;
    st = (st + nxt + 3) % 3;
  }
  return ret;
}
void solve() {
  int ans = 222 * 222;
  for (int st = 0; st < 3; st++) ans = min(ans, go(st, 1));
  printf("%d\n", ans);
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
