#include <bits/stdc++.h>
using namespace std;
const int N = 207;
int n, a[N], c[N], ind[N];
vector<int> e[N];
int solve(int p) {
  queue<int> que[3];
  for (int i = (0); i < (n); ++i) ind[i] = a[i];
  for (int i = (0); i < (n); ++i)
    if (!ind[i]) que[c[i]].push(i);
  int ret = 0;
  while (((int)(que[0]).size()) + ((int)(que[1]).size()) +
         ((int)(que[2]).size())) {
    while (!que[p].empty()) {
      int x = que[p].front();
      ++ret, que[p].pop();
      for (int i = (0); i < (((int)(e[x]).size())); ++i)
        if (--ind[e[x][i]] == 0) que[c[e[x][i]]].push(e[x][i]);
    }
    ++ret, p = (p + 1) % 3;
  }
  return ret - 1;
}
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) {
    scanf("%d", &a[i]);
    for (int j = (0); j < (a[i]); ++j) {
      int x;
      scanf("%d", &x);
      e[x - 1].push_back(i);
    }
  }
  int ans = 1e9 + 7;
  for (int i = (0); i < (3); ++i) ans = min(ans, solve(i));
  printf("%d", ans);
  return 0;
}
