[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 vv be an int vector of size 210
let computer, in, bk, temp be integer vectors of size 210 each
in int function bfs which takes int st
    let q be an int queue
    let ans = n = integer
    for i = 1 to n inclusive
        if in at i is 0 then push i into q
    end of the block
    while q isn't empty
        reset all bk to 0
        while q isn't empty
            head = front of q = integer
            pop q
            if bk at head is 5
                push head in q
                exit loop
            end of the block
            increment bk at head
            if computer at head is not st
                push head into q
                next iteration
            end of the block
            let up be an integer = the size of vv at head
            for i = 0 to up exclusive
                let the integer v be vv[head][i]
                decrement in at v
                if in at v is 0 then push v into q
            end of the block
        end of the block
        set st = 1 if st is 3 else increment st
        if q isn't empty then increment ans
    end of the block
    return ans
end of the block
define the main function
    while reading n
        reset all in to 0
        clear vv form 1 to n inclusive
        read computer from 1 to n inclusive
        for i = 1 to n inclusive
            let ki be an int
            read ki
            for j = 1 to ki inclusive
                declare int aj
                read aj
                add i to the end of vv[aj]
                increment in at i
            end of the block
        end of the block
        let ans be an integer = 0x3f3f3f3f
        for i = 1 to n inclusive, set temp at i to in at i
        for i = 1 to 3 inclusive
            for j = 1 to n inclusive
            set ans to the minimum between ans and bfs of i
        end of the block
        print ans
    end of the block
    return to indicate successful execution
end of the block

[CODE]:

int n;
vector<int> vv[210];
int computer[210], in[210], bk[210], temp[210];
int bfs(int st) {
    queue<int> q;
    int ans = n;
    for (int i = 1; i <= n; i++) {
        if (in[i] == 0) q.push(i);
    }
    while (!q.empty()) {
        memset(bk, 0, sizeof(bk));
        while (!q.empty()) {
            int head = q.front();
            q.pop();
            if (bk[head] == 5) {
                q.push(head);
                break;
            }
            bk[head]++;
            if (computer[head] != st) {
                q.push(head);
                continue;
            }
            int up = vv[head].size();
            for (int i = 0; i < up; i++) {
                int v = vv[head][i];
                in[v]--;
                if (in[v] == 0) q.push(v);
            }
        }
        st = (st == 3 ? 1 : st + 1);
        if (!q.empty()) ans++;
    }
    return ans;
}
int main() {
    while (cin >> n) {
        memset(in, 0, sizeof(in));
        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);
                in[i]++;
            }
        }
        int ans = 0x3f3f3f3f;
        for (int i = 1; i <= n; i++) temp[i] = in[i];
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= n; j++) in[j] = temp[j];
            ans = min(ans, bfs(i));
        }
        cout << ans << endl;
    }
    return 0;
}