[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]:
n=int
computer and vis=array of 201 int, check=array of 3 by 201 int
vv=array of 201 vector of int
function bfs (get int k and now, return int)
    q=queue of int
    ans=n
    for i=1 to n inclusive
        if check[k][i] is 0 push i into q
    end of the block
    while q is not empty
        fill vis with 0
        while q is not empty
            head = first item in q
            pop q
            if vis[head] is n
                push head into q
                break
            end of the block
            increment vis[head]
            if computer[head] is not now
                push head into q
                continue
            end of the block
            for i=0 to size of vv[head] exclusive
                decrement check[k][vv[head][i]]
                if check[k][vv[head][i]] is 0 push vv[head][i] into q
            end of the block
        end of the block
        now = 1 if now is 3 else now+1
        if q is not empty increment ans
    end of the block
    return ans
end of the block
define the main function
    while read n
        fill check with 0
        for i=1 to n inclusive clear vv[i]
        for i=1 to n inclusive read compute[i]
        for i=1 to n inclusive
            ki=int
            read ki
            for j=1 to ki inclusive
                aj=int
                read aj
                add i to end of vv[aj]
                increment check[0][i],check[1][i],check[2][i]
            end of the block
        end of the block
        ans=3f3f3f3f in exadecimal
        for i=1 to 3 exclusive ans=min(ans, bfs(i-1,i))
        print ans
    end of the block
    return to indicate successful execution
end of the block

[CODE]:

int n;
int computer[201], check[3][201], vis[201];
vector<int> vv[201];
int bfs(int k, int now) {
    queue<int> q;
    int ans = n;
    for (int i = 1; i <= n; i++) {
        if (check[k][i] == 0) q.push(i);
    }
    while (!q.empty()) {
        memset(vis, 0, sizeof(vis));
        while (!q.empty()) {
            int head = q.front();
            q.pop();
            if (vis[head] == n) {
                q.push(head);
                break;
            }
            vis[head]++;
            if (computer[head] != now) {
                q.push(head);
                continue;
            }
            for (int i = 0; i < vv[head].size(); i++) {
                check[k][vv[head][i]]--;
                if (check[k][vv[head][i]] == 0) q.push(vv[head][i]);
            }
        }
        now = (now == 3 ? 1 : now + 1);
        if (!q.empty()) ans++;
    }
    return ans;
}
int main() {
    while (cin >> n) {
        memset(check, 0, sizeof(check));
        for (int i = 1; i <= n; i++) vv[i].clear();
        for (int i = 1; i <= n; i++) cin >> computer[i];
        for (int i = 1; i <= n; i++) {
            int ki;
            cin >> ki;
            for (int j = 1; j <= ki; j++) {
                int aj;
                cin >> aj;
                vv[aj].push_back(i);
                check[0][i]++, check[1][i]++, check[2][i]++;
            }
        }
        int ans = 0x3f3f3f3f;
        for (int i = 1; i <= 3; i++) ans = min(ans, bfs(i - 1, i));
        cout << ans << endl;
    }
    return 0;
}