#include <bits/stdc++.h>
using namespace std;
struct node {
  int v, nex;
} side[5020000];
int head[300], cnt = 0;
int book[300];
int in[300];
int ans[300];
int ind[300];
void init() {
  cnt = 0;
  memset(head, -1, sizeof(head));
  memset(book, 0, sizeof(book));
}
void add(int x, int y) {
  side[cnt].v = y;
  side[cnt].nex = head[x];
  head[x] = cnt++;
}
int wi[300];
int a[300];
int graph[5][5];
int main() {
  init();
  memset(graph, 0x3f3f3f3f, sizeof(graph));
  graph[1][1] = 0;
  graph[2][2] = 0;
  graph[3][3] = 0;
  graph[1][2] = 1;
  graph[1][3] = 2;
  graph[2][1] = 2;
  graph[2][3] = 1;
  graph[3][2] = 2;
  graph[3][1] = 1;
  for (int k = 1; k <= 3; k++) {
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        if (graph[i][j] > graph[i][k] + graph[k][j]) {
          graph[i][j] = graph[i][k] + graph[k][j];
        }
      }
    }
  }
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    int ki;
    scanf("%d", &ki);
    for (int j = 0; j < ki; j++) {
      int u;
      scanf("%d", &u);
      add(u, i);
      in[i]++;
    }
  }
  for (int i = 1; i <= n; i++) {
    ind[i] = in[i];
  }
  int ee = 5000000;
  for (int zz = 1; zz <= 3; zz++) {
    memset(ans, 0, sizeof(ans));
    for (int i = 1; i <= n; i++) {
      in[i] = ind[i];
    }
    priority_queue<int, vector<int>, greater<int> > que, que2, que3;
    for (int i = 1; i <= n; i++) {
      if (in[i] == 0) {
        if (a[i] == 1) {
          que.push(i);
        } else if (a[i] == 2) {
          que2.push(i);
        } else {
          que3.push(i);
        }
      }
    }
    priority_queue<int, vector<int>, greater<int> > q1;
    cnt = 0;
    int wi = zz - 1;
    while (!que.empty() || !que2.empty() || !que3.empty()) {
      wi++;
      if (wi == 4) wi = 1;
      if (wi == 1) {
        if (que.empty()) continue;
        while (!que.empty()) {
          q1.push(que.top());
          que.pop();
        }
      } else if (wi == 2) {
        if (que2.empty()) continue;
        while (!que2.empty()) {
          q1.push(que2.top());
          que2.pop();
        }
      } else {
        if (que3.empty()) continue;
        while (!que3.empty()) {
          q1.push(que3.top());
          que3.pop();
        }
      }
      while (!q1.empty()) {
        int t = q1.top();
        ans[cnt++] = t;
        q1.pop();
        for (int i = head[t]; i != -1; i = side[i].nex) {
          int y = side[i].v;
          in[y]--;
          if (in[y] <= 0) {
            if (a[t] == a[y])
              q1.push(y);
            else {
              if (a[y] == 1) {
                que.push(y);
              } else if (a[y] == 2) {
                que2.push(y);
              } else {
                que3.push(y);
              }
            }
          }
        }
      }
    }
    sum = 0;
    for (int i = 0; i < cnt - 1; i++) {
      sum += graph[a[ans[i]]][a[ans[i + 1]]];
    }
    ee = min(ee, sum);
  }
  printf("%d\n", ee + n);
  return 0;
}
