[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]:
let n be an int
let computer be an integer array of length 201, check an integer matrix of size 3x201, vis an integer array of length 201
let vv be an integer vector of size 201
in int function bsf that takes int num, int st
    let q be a queue of int
    let ans = n be an int
    for i = 1 to n inclusive
        if che[num][i] is 0, push i in q
    end of the block
    while q is not empty
        reset vis to all 0
        while q is not empty
            let head = int = result of calling front on q
            pop q
            if vis at head is n
                push head in q
                exit loop
            end of the block
            increment vis[head]
            if computer at head is not st
                push head in q
                next iteration
            end of the block
            for i = 0 to size of vv[head] exclusive
                let v = vv[head][i] be an int
                decrement check[num][v]
                if check[num][v] is 0 then push v in q
            end of the block
        end of the block
        set st to 1 if st is 3 else set st to st + 1
        if q is not empty then increment ans
    end of the block
    return ans
end of the block
define the main function
    while reading n
        reset check to all 0
        clear n elements of vv[i]
        read n elements of computer
        for i = 1 to n inclusive
            let ki = integer
            read ki
            for j = 1 to ki inclusive
                let aj be an integer
                read aj
                add i to the end of vv[aj]
                increment check[0][i]++, check[1][i]++, check[2][i]++
            end of the block
        end of the block
        let ans be an int = 0x3f3f3f3f
        for i = 1 to 3 inclusive, set ans to the minimum between ans and bfs of i - 1 and 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 num, int st) {
    queue<int> q;
    int ans = n;
    for (int i = 1; i <= n; i++) {
        if (check[num][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] != st) {
                q.push(head);
                continue;
            }
            for (int i = 0; i < vv[head].size(); i++) {
                int v = vv[head][i];
                check[num][v]--;
                if (check[num][v] == 0) q.push(v);
            }
        }
        st = (st == 3 ? 1 : st + 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;
}