[TASK]:
You will be given lines of pseudocode, your task is to write the corresponding C++ code. The pseudocode will provide detailed description of the c++ code line by line. The pseudocode is garanteed to be correct and complete.

[INSTRUCTION]:
The following libraries are already included in the code.
```cpp
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <fstream>
#include <climits>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <bitset>
using namespace std;
```
Do not include them in your code again. Please surround your code with ```cpp and ``` markers. Note that the code should correspond to the pseudocode line by line.

[PSEUDOCODE]:
create const int N = 205
declare integer variable n and arrays of integers in, tin and a with size N
declare vector of integers e
bfs is an int function with int argument k
    Q is a queue of integers with 5 elements
    for integer i = 1 to n inclusive
        if in[i] is false, push i into Q[a[i]]
    for i = 1 to n inclusive, change tin[i] to in[i]
    declare int step = -1
    while any of Q[1], Q[2] or Q[3] is not empty
        increment step by one
        while Q[k] is not empty
            declare integer variable u = front element of Q[k]
            remove front element from Q[k]
            increment step
            for i from 0 to length of e[u] exclusive
                declare int v = e[u][i]
                decrement tin[v] by one
                if tin[v] is false, push v into Q[a[v]]
            end of the block
        end of the block
        if k = 1
            assign 2 to k
        else if k = 2
            assign 3 to k
        else if k = 3
            assign 1 to k
    end of the block
    if step != -1, return step
    return to indicate successful execution
end of the block
define the main function
    read n
    for i = 1 to n inclusive, read a[i]
    for integers i = 1, m and x, while i <= n, incrementing i
        read input to m
        change in[i] to m
        decrement m and loop further, while m is not 0
            read x
            push i into e[x]
        end of the block
    end of the block
    declare integer variable ans = 0x3f3f3f3f
    for i = 1 to 3 inclusive, set ans to min of ans and bfs(i)
    print ans
    return to indicate successful execution
end of the block

[CODE]:

const int N = 205;
int in[N], tin[N], n, a[N];
vector<int> e[N];
int bfs(int k) {
    queue<int> Q[5];
    for (int i = 1; i <= n; i++)
        if (!in[i]) Q[a[i]].push(i);
    for (int i = 1; i <= n; i++) tin[i] = in[i];
    int step = -1;
    while (!Q[1].empty() || !Q[2].empty() || !Q[3].empty()) {
        step++;
        while (!Q[k].empty()) {
            int u = Q[k].front();
            Q[k].pop();
            step++;
            for (int i = 0; i < e[u].size(); i++) {
                int v = e[u][i];
                tin[v]--;
                if (!tin[v]) Q[a[v]].push(v);
            }
        }
        if (k == 1)
            k = 2;
        else if (k == 2)
            k = 3;
        else if (k == 3)
            k = 1;
    }
    if (step != -1) return step;
    return 0;
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1, m, x; i <= n; i++) {
        cin >> m;
        in[i] = m;
        while (m--) {
            cin >> x;
            e[x].push_back(i);
        }
    }
    int ans = 0x3f3f3f3f;
    for (int i = 1; i <= 3; i++) ans = min(ans, bfs(i));
    cout << ans << endl;
    return 0;
}