#include <bits/stdc++.h>
using namespace std;
int n;
int gcd(int a, int b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}
const int INF = 1000000000;
vector<int> c;
vector<vector<int> > g;
vector<int> rm;
int comp[4][4];
vector<int> roots;
int main() {
  cin >> n;
  rm.resize(n, 0);
  c.resize(n);
  g.resize(n);
  vector<int> anss;
  comp[1][1] = comp[2][2] = comp[3][3] = 0;
  comp[1][2] = 1;
  comp[1][3] = 2;
  comp[2][3] = 1;
  comp[3][2] = 2;
  comp[2][1] = 2;
  comp[3][1] = 1;
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      int u;
      cin >> u;
      g[i].push_back(u - 1);
    }
  }
  for (int i = 0; i < n; i++)
    if (g[i].size() == 0) roots.push_back(i);
  int final_ans = INF;
  for (int q = 0; q < roots.size(); q++) {
    int curc = c[roots[q]];
    rm[roots[q]] = 1;
    int movec = 0;
    int ans = 1;
    for (int i = 0; i < n; i++) {
      bool check = false;
      for (int p = 0, movec = curc - 1; p < 3; p++, movec += 1, movec %= 3) {
        for (int j = 0; j < n; j++) {
          if (rm[j] || c[j] != movec + 1) continue;
          check = true;
          for (int k = 0; k < g[j].size(); k++) {
            if (!rm[g[j][k]]) check = false;
          }
          if (check) {
            rm[j] = 1;
            ans += comp[curc][movec + 1] + 1;
            curc = movec + 1;
            break;
          }
        }
        if (check) break;
      }
    }
    int check = 1;
    for (int i = 0; i < n; i++) check &= rm[i];
    rm.assign(n, 0);
    if (check) final_ans = min(final_ans, ans);
  }
  cout << final_ans;
}
