#include <bits/stdc++.h>
using namespace std;
vector<int> adj[280];
int com[280], n, dir[280], in[280];
bool mark[280];
int go(int a) {
  vector<int> ad[280];
  for (int i = 0; i < n; i++) ad[i] = adj[i];
  int inn[280];
  for (int i = 0; i < n; i++) inn[i] = in[i];
  memset(mark, 0, sizeof mark);
  int sum = 0, node = 0, cnt = 0;
  while (node < n) {
    while (1) {
      bool f = 1;
      for (int j = 0; j < n; j++) {
        if (com[j] == dir[a] && inn[j] == 0 && !mark[j]) {
          mark[j] = 1;
          f = 0;
          for (int k = 0; k < ad[j].size(); k++) {
            inn[ad[j][k]]--;
            ad[j].erase(ad[j].begin() + k);
            k--;
          }
          node++;
        }
      }
      if (f) {
        cnt++;
        break;
      }
      cnt = 0;
    }
    if (cnt == 3) return 200000;
    sum++;
    a++;
  }
  return sum - cnt;
}
int main() {
  int k = 1;
  for (int i = 0; i < 280; i++) {
    dir[i] = k++;
    if (k == 4) k = 1;
  }
  memset(in, 0, sizeof in);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> com[i];
  for (int i = 0; i < n; i++) {
    int t, x;
    cin >> t;
    while (t--) {
      cin >> x;
      x--;
      adj[x].push_back(i);
      in[i]++;
    }
  }
  int mn = go(0);
  mn = min(mn, go(1));
  mn = min(mn, go(2));
  cout << mn + n << endl;
  return 0;
}
