#include <bits/stdc++.h>
using namespace std;
int a[210];
int in[210], conn[210];
queue<int> path[3];
vector<int> graf[210];
int n;
int solve(int x) {
  int ret = 0;
  int fin = 0;
  memset(conn, 0, sizeof conn);
  for (int i = 0; i < n; i++)
    if (in[i] == 0) path[a[i]].push(i);
  while (fin != n) {
    while (!path[x].empty()) {
      int curr = path[x].front();
      ret++;
      fin++;
      path[x].pop();
      for (int i = 0; i < graf[curr].size(); i++) {
        conn[graf[curr][i]]++;
        if (conn[graf[curr][i]] == in[graf[curr][i]])
          path[a[graf[curr][i]]].push(graf[curr][i]);
      }
    }
    if (fin == n) return ret;
    x++;
    x %= 3;
    ret++;
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    int k, x;
    scanf("%d", &k);
    for (int j = 0; j < k; j++) {
      scanf("%d", &x);
      graf[x - 1].push_back(i);
      in[i]++;
    }
  }
  printf("%d\n", min(solve(0), min(solve(1), solve(2))));
  return 0;
}
