#include <bits/stdc++.h>
using namespace std;
int comp[205];
int pre[205][205];
int main() {
  memset(comp, 0, sizeof comp);
  memset(pre, 0, sizeof pre);
  int N;
  cin >> N;
  for (int i = 0; i < int(N); ++i) cin >> comp[i];
  int start[3] = {-1, -1, -1};
  for (int i = 0; i < int(N); ++i) {
    int k;
    cin >> k;
    for (int j = 0; j < int(k); ++j) {
      int t;
      cin >> t;
      t--;
      pre[i][t] = 1;
    }
    if (!k) start[comp[i] - 1] = i;
  }
  int best = 99999;
  for (int s = 0; s < int(3); ++s) {
    if (start[s] == -1) continue;
    for (int i = 0; i < int(N); ++i) {
      pre[i][i] = 0;
      pre[i][i] = accumulate(pre[i], pre[i] + N, 0);
    }
    queue<int> q[3];
    int time = 0, curc = s;
    q[s].push(start[s]);
    while (q[0].size() || q[1].size() || q[2].size()) {
      if (q[curc].size()) {
        int top = q[curc].front();
        q[curc].pop();
        pre[top][top] = -1;
        for (int i = 0; i < int(N); ++i) {
          if (pre[i][top]) pre[i][i]--;
          if (pre[i][i] == 0) pre[i][i] = -1, q[comp[i] - 1].push(i);
        }
      } else {
        curc = (curc + 1) % 3;
        time++;
      }
    }
    best = min(best, time + N);
  }
  cout << best << endl;
  return 0;
}
