#include <bits/stdc++.h>
using namespace std;
vector<int> tu[1005];
int a[1005], cnt[1005], n, checks[1005];
int check(int x) {
  int i, k;
  int sum = -1;
  queue<int> qua[5];
  for (i = 1; i <= n; i++) {
    if (cnt[i] == 0) {
      qua[a[i]].push(i);
    }
  }
  for (;;) {
    if (qua[1].size() == 0 && qua[2].size() == 0 && qua[3].size() == 0) break;
    while (!qua[x].empty()) {
      sum++;
      k = qua[x].front();
      qua[x].pop();
      for (i = 0; i < tu[k].size(); i++) {
        int xx = tu[k][i];
        cnt[xx]--;
        if (cnt[xx] == 0) qua[a[xx]].push(xx);
      }
    }
    if (x == 1)
      x = 2;
    else if (x == 2)
      x = 3;
    else if (x == 3)
      x = 1;
    sum++;
  }
  if (sum == -1)
    return 0;
  else
    return sum;
}
int main() {
  int i;
  int ans, T, x;
  cin >> n;
  memset(a, 0, sizeof(a));
  memset(tu, 0, sizeof(tu));
  memset(cnt, 0, sizeof(cnt));
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 1; i <= n; i++) {
    cin >> T;
    while (T--) {
      cin >> x;
      tu[x].push_back(i);
      checks[i]++;
    }
  }
  ans = 1 << 30;
  int j;
  for (i = 1; i <= 3; i++) {
    for (j = 1; j <= n; j++) {
      cnt[j] = checks[j];
    }
    ans = min(ans, check(i));
  }
  cout << ans << endl;
}
