#include <bits/stdc++.h>
using namespace std;
const int N = 205;
vector<int> graph[N];
int c[N], cnt[N], mem_c[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    c[i]--;
  }
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int from;
      cin >> from;
      graph[from].push_back(i);
      mem_c[i]++;
    }
  }
  int ans, res, out = int(1e9);
  for (int i = 0; i < 3; i++) {
    ans = 0, res = 0;
    for (int j = 1; j <= n; j++) cnt[j] = mem_c[j];
    for (int color = i;; color = (color + 1) % 3) {
      bool ch = 1;
      while (ch) {
        ch = 0;
        for (int j = 1; j <= n; j++) {
          if (c[j] == color && !cnt[j]) {
            for (int t = 0; t < (int)(graph[j]).size(); t++) {
              int v = graph[j][t];
              cnt[v]--;
            }
            cnt[j]--;
            ch = 1;
            ans++;
            res++;
          }
        }
      }
      if (res == n) break;
      ans++;
    }
    out = min(out, ans);
  }
  cout << out << endl;
  return 0;
}
