#include <bits/stdc++.h>
using namespace std;
vector<int> v[205];
int indegree[205];
int main() {
  int n;
  scanf("%d", &n);
  int ddo[205];
  for (int i = 1; i <= n; i++) scanf("%d", &ddo[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &indegree[i]);
    for (int j = 0; j < indegree[i]; j++) {
      int first;
      scanf("%d", &first);
      v[first].push_back(i);
    }
  }
  queue<int> q[4];
  int ans = 1e9;
  for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= n; j++) {
      if (!indegree[j]) q[ddo[j]].push(j);
    }
    int cnt = 0;
    int ind[205];
    for (int j = 0; j < 205; j++) ind[j] = indegree[j];
    int now = i;
    int res = 0;
    while (cnt != n) {
      while (!q[now].empty()) {
        int first = q[now].front();
        q[now].pop();
        for (vector<int>::iterator it = v[first].begin(); it != v[first].end();
             it++) {
          if (--ind[*it] == 0) {
            q[ddo[*it]].push(*it);
          }
        }
        res++;
        cnt++;
      }
      if (cnt == n) break;
      res++;
      now = now % 3 + 1;
    }
    ans = min(ans, res);
  }
  printf("%d\n", ans);
}
