#include <bits/stdc++.h>
using namespace std;
int n, b[205], d[205], o[205], ans;
vector<int> a[205], q[4];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
  for (int i = 1; i <= n; ++i) {
    int k;
    scanf("%d", &k);
    for (int j = 1; j <= k; ++j) {
      int t;
      scanf("%d", &t);
      a[t].push_back(i);
      ++d[i];
    }
    o[i] = d[i];
  }
  ans = 1 << 30;
  for (int k = 1; k <= 3; ++k) {
    int j = k, tot = 0, now = n - 1;
    for (int i = 1; i <= n; ++i) {
      d[i] = o[i];
      if (!d[i]) q[b[i]].push_back(i);
    }
    while (tot < n) {
      for (int i = 0; i < (int)q[j].size(); ++i) {
        int rt = q[j][i];
        tot++;
        for (int k = 0; k < (int)a[rt].size(); k++) {
          int p = a[rt][k];
          d[p]--;
          if (!d[p]) q[b[p]].push_back(p);
        }
      }
      q[j].clear();
      j = j % 3 + 1;
      now++;
    }
    ans = min(ans, now);
  }
  printf("%d\n", ans);
  return 0;
}
