#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const double PI = 3.141592653589793238462643383279;
int c[301];
vector<int> v[301];
int deg[301], tmp[301];
int main() {
  int n;
  cin >> n;
  for (int ni = 1; ni <= n; ni++) cin >> c[ni];
  for (int ni = 1; ni <= n; ni++) {
    int k;
    scanf("%d", &k);
    for (int ki = 0; ki < k; ki++) {
      int x;
      cin >> x;
      v[x].push_back(ni);
      deg[ni]++;
    }
  }
  int ans = 3 * n;
  for (int i = 0; i < 3; i++) {
    int st = i;
    queue<int> que[3];
    for (int ni = 1; ni <= n; ni++) {
      tmp[ni] = deg[ni];
      if (!deg[ni]) que[c[ni] - 1].push(ni);
    }
    int cnt = 0;
    int hr = 0;
    while (cnt != n) {
      queue<int> &q = que[st];
      while (!q.empty()) {
        int first = q.front();
        q.pop();
        hr += 1;
        cnt += 1;
        for (int x : v[first]) {
          if (!--tmp[x]) que[c[x] - 1].push(x);
        }
      }
      if (cnt != n) {
        hr += 1;
        st = (st + 1) % 3;
      }
    }
    ans = min(ans, hr);
  }
  cout << ans << endl;
}
