#include <bits/stdc++.h>
using namespace std;
int n, d[205], loc[205], ch[][3] = {{1, 2, 3}, {3, 1, 2}, {2, 3, 1}}, deg[205],
                         tempd[205];
bool ok[205] = {};
vector<int> adj[205];
int main() {
  memset(d, -1, sizeof(d));
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> loc[i];
    loc[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> deg[i];
    tempd[i] = deg[i];
    for (int j = 0; j < deg[i]; j++) {
      int l;
      cin >> l;
      l--;
      adj[l].push_back(i);
    }
  }
  int res[3] = {};
  for (int a = 0; a < 3; a++) {
    memset(ok, 0, sizeof(ok));
    for (int i = 0; i < n; i++) deg[i] = tempd[i];
    for (int i = 0; i < 605; i++) {
      for (int asdf = 0; asdf < n; asdf++)
        for (int j = 0; j < n; j++) {
          if ((i + a) % 3 == loc[j]) {
            if (deg[j] == 0 && !ok[j]) {
              ok[j] = 1;
              for (int k = 0; k < adj[j].size(); k++) deg[adj[j][k]]--;
            }
          }
        }
      bool fin = 1;
      for (int j = 0; j < n; j++)
        if (!ok[j]) fin = 0;
      if (fin) break;
      res[a]++;
    }
  }
  cout << min(res[0], min(res[1], res[2])) + n << endl;
}
