[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]:
tu = int vector array of size 1005
a, cnt, checks = int array of size 1005 each and n = int
in function check taking an int x and returning an in
    i, k = int
    sum = int with sum = -1
    qua = int queue array of size 5
    for i = 1 to n inclusive
        if cnt[i] is 0 then push i to qua[a[i]]
    end of the block
    loop infinitely
        if size of qua[1] or size of qua[2] or size of qua[3] is 0 break
        loop while qua[x] is not empty
            increment sum
            set k to front of qua[x]
            pop qua[x]
            for i = 0 to size of tu[k]
                xx = int with xx = tu[k][i]
                decrement cnt[xx]
                if cnt[xx] is 0 push xx to qua[a[xx]]
            end of the block
        end of the block
        if x is 1
            set x to 2
        else if x is 2
            set x to 3
        else if x is 3
            set x to 1
        increment sum
    end of the block
    if sum is -1
        return to indicate successful execution
    else
        return sum
end of the block
define the main function
    i = int
    ans, T, x = int
    read n
    set all values of a to 0
    set all values of tu to 0
    set all values of cnt to 0
    for i = 1 to n inclusive read a[i]
    for i = 1 to n inclusive
        read T
        loop T times
            read x
            append i to tu[x]
            increment checks[i]
        end of the block
    end of the block
    set ans to 1 bitshift right by 30
    j = int
    for i = 1 to 3 inclusive
        for j = 1 to n inclusive set cnt[j] to checks[j]
        set ans to min of ans and check of i
    end of the block
    set ans
end of the block

[CODE]:

vector<int> tu[1005];
int a[1005], cnt[1005], n, checks[1005];
int check(int x) {
    int i, k;
    int sum = -1;
    queue<int> qua[5];
    for (i = 1; i <= n; i++) {
        if (cnt[i] == 0) { qua[a[i]].push(i); }
    }
    for (;;) {
        if (qua[1].size() == 0 && qua[2].size() == 0 && qua[3].size() == 0) break;
        while (!qua[x].empty()) {
            sum++;
            k = qua[x].front();
            qua[x].pop();
            for (i = 0; i < tu[k].size(); i++) {
                int xx = tu[k][i];
                cnt[xx]--;
                if (cnt[xx] == 0) qua[a[xx]].push(xx);
            }
        }
        if (x == 1)
            x = 2;
        else if (x == 2)
            x = 3;
        else if (x == 3)
            x = 1;
        sum++;
    }
    if (sum == -1)
        return 0;
    else
        return sum;
}
int main() {
    int i;
    int ans, T, x;
    cin >> n;
    memset(a, 0, sizeof(a));
    memset(tu, 0, sizeof(tu));
    memset(cnt, 0, sizeof(cnt));
    for (i = 1; i <= n; i++) { cin >> a[i]; }
    for (i = 1; i <= n; i++) {
        cin >> T;
        while (T--) {
            cin >> x;
            tu[x].push_back(i);
            checks[i]++;
        }
    }
    ans = 1 << 30;
    int j;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= n; j++) { cnt[j] = checks[j]; }
        ans = min(ans, check(i));
    }
    cout << ans << endl;
}