#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int pos[300];
vector<int> x[300];
int re1[300];
int re2[300];
int re3[300];
queue<int> nm1[5];
queue<int> nm2[5];
queue<int> nm3[5];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> pos[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> m;
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      x[a].push_back(i);
    }
    re1[i] += m;
    re2[i] += m;
    re3[i] += m;
    if (re1[i] == 0) {
      nm1[pos[i]].push(i);
      nm2[pos[i]].push(i);
      nm3[pos[i]].push(i);
    }
  }
  int minn = (int)(INT_MAX - 1);
  int u = -1;
  int loc = 1;
  int mo = 0;
  if (!nm1[1].empty()) {
    while (!nm1[1].empty() || !nm1[2].empty() || !nm1[3].empty()) {
      u = nm1[loc].front();
      nm1[loc].pop();
      mo++;
      for (int i = 0; i < x[u].size(); i++) {
        int v = x[u][i];
        re1[v]--;
        if (re1[v] == 0) {
          nm1[pos[v]].push(v);
        }
      }
      if (nm1[loc].empty()) {
        loc += 1;
        if (loc > 3) loc -= 3;
        if (nm1[loc].empty()) {
          loc += 1;
          if (loc > 3) loc -= 3;
          if (!nm1[loc].empty()) mo += 2;
        } else {
          mo += 1;
        }
      }
    }
    minn = min(minn, mo);
  }
  loc = 2;
  mo = 0;
  if (!nm2[2].empty()) {
    while (!nm2[1].empty() || !nm2[2].empty() || !nm2[3].empty()) {
      u = nm2[loc].front();
      nm2[loc].pop();
      mo++;
      for (int i = 0; i < x[u].size(); i++) {
        int v = x[u][i];
        re2[v]--;
        if (re2[v] == 0) {
          nm2[pos[v]].push(v);
        }
      }
      if (nm2[loc].empty()) {
        loc += 1;
        if (loc > 3) loc -= 3;
        if (nm2[loc].empty()) {
          loc += 1;
          if (loc > 3) loc -= 3;
          if (!nm2[loc].empty()) mo += 2;
        } else {
          mo += 1;
        }
      }
    }
    minn = min(minn, mo);
  }
  loc = 3;
  mo = 0;
  if (!nm3[3].empty()) {
    while (!nm3[1].empty() || !nm3[2].empty() || !nm3[3].empty()) {
      u = nm3[loc].front();
      nm3[loc].pop();
      mo++;
      for (int i = 0; i < x[u].size(); i++) {
        int v = x[u][i];
        re3[v]--;
        if (re3[v] == 0) {
          nm3[pos[v]].push(v);
        }
      }
      if (nm3[loc].empty()) {
        loc += 1;
        if (loc > 3) loc -= 3;
        if (nm3[loc].empty()) {
          loc += 1;
          if (loc > 3) loc -= 3;
          if (!nm3[loc].empty()) mo += 2;
        } else {
          mo += 1;
        }
      }
    }
    minn = min(minn, mo);
  }
  cout << minn << endl;
  return 0;
}
