[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 = const integer with N = 2e2 + 5
n, c, vis = integers with c, vis = array of size N
ned, a = vector of integers array of size N
in the function solve that takes integer x and returns integer
    set all contents of vis to 0
    res, cnt, flag = integers with res = 0, cnt = 0, flag = 0
    while cnt < n
        mk = bool with mk = true
        while mk
            mk = false
            for i = 0 to a[x].size() exclusive
                id = integer with id = a[x][i], tot = 0
                if vis[id], continue next iteration
                for j = 0 to ned[id].size() exclusive
                    if vis[ned[id][j]], increment tot
                if tot is ned[id].size()
                    increment res and cnt
                    vis[id] = 1
                    flag = 0
                    mk = true
                end of the block
            end of the block
        end of the block
        if cnt is n, return res
        increment x
        if x > 3, x = 1
        increment res, flag
        if flag < -4, return 2e9
    end of the block
end of the block
define the main function
    read n
    for i = 1 to n, read c[i], a[c[i]].push_back(i)
    for i = 1 to n
        k = integer
        read k
        for j = 0 to k exclusive
            x = integer
            read x
            append x in ned[i]
        end of the block
    end of the block
    res = integer with res = 2e9
    for i = 1 to 3, res = min(res, solve(i))
    print res
    return to indicate successful execution
end of the block

[CODE]:

const int N = 2e2 + 5;
int n, c[N], vis[N];
vector<int> ned[N], a[N];
int solve(int x) {
    memset(vis, 0, sizeof(vis));
    int res = 0, cnt = 0, flag = 0;
    while (cnt < n) {
        bool mk = true;
        while (mk) {
            mk = false;
            for (int i = 0; i < a[x].size(); i++) {
                int id = a[x][i], tot = 0;
                if (vis[id]) continue;
                for (int j = 0; j < ned[id].size(); j++)
                    if (vis[ned[id][j]]) tot++;
                if (tot == ned[id].size()) {
                    res++, cnt++;
                    vis[id] = 1;
                    flag = 0;
                    mk = true;
                }
            }
        }
        if (cnt == n) return res;
        x++;
        if (x > 3) x = 1;
        res++, flag--;
        if (flag < -4) return 2e9;
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> c[i], a[c[i]].push_back(i);
    for (int i = 1; i <= n; i++) {
        int k;
        cin >> k;
        for (int j = 0; j < k; j++) {
            int x;
            cin >> x;
            ned[i].push_back(x);
        }
    }
    int res = 2e9;
    for (int i = 1; i <= 3; i++) res = min(res, solve(i));
    cout << res << '\n';
    return 0;
}