[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]:
declare int N and an arrays of ints a and c with 200 elements
declare an array of int vectors called edges with 200 elements
int function simulate with int argument s
    set first sizeof(c) bytes at the pointer c to 0
    for i = 0 to N exclusive
        for j = 0 to length of edges[i] exclusive, increment c[edges[i][j]]
    end of the block
    q is a new array of int queues with 3 elements
    for integer i = 0 to N exclusive
        if c[i] is false, push i into q[a[i]]
    create integer poped = 0
    declare int variable ret = -1
    while poped < N
        while q[s] is not empty
            declare int variable f = front element of q[s]
            remove front element from q[s]
            increment poped by one
            for i = 0 to length of edges[f] exclusive
                create int next = edges[f][i]
                decrement c[next], if c[next] != 0, push next into q[a[next]]
            end of the block
        end of the block
        assign (s + 1) % 3 to s
        increment ret by one
    end of the block
    return ret
end of the block
define the main function
    read N and loop further
        for i = 0 to N exclusive
        for integer i = int to N exclusive
            read input to a[i]
            a[i] = a[i] modulo 3
        end of the block
        for integer i = 0 to N exclusive
            declare ints k and t
            read k
            for j = 0 to k exclusive
                read input to t
                push i into edges[t - 1]
            end of the block
        end of the block
        create integer ans = 1000000000
        for integer i = 0 to 3 exclusive, set ans to min(ans, simulate(i))
        print ans + N
    end of the block
    return to indicate successful execution
end of the block

[CODE]:

int N, a[200], c[200];
vector<int> edges[200];
int simulate(int s) {
    memset(c, 0, sizeof(c));
    for (int i = int(0); i < int(N); i++) {
        for (int j = int(0); j < int(edges[i].size()); j++) c[edges[i][j]]++;
    }
    queue<int> q[3];
    for (int i = int(0); i < int(N); i++)
        if (!c[i]) q[a[i]].push(i);
    int poped = 0;
    int ret = -1;
    while (poped < N) {
        while (!q[s].empty()) {
            int f = q[s].front();
            q[s].pop();
            poped++;
            for (int i = int(0); i < int(edges[f].size()); i++) {
                int next = edges[f][i];
                if (!--c[next]) { q[a[next]].push(next); }
            }
        }
        s = (s + 1) % 3;
        ret++;
    }
    return ret;
}
int main() {
    while (cin >> N) {
        for (int i = int(0); i < int(N); i++) edges[i].clear();
        for (int i = int(0); i < int(N); i++) {
            cin >> a[i];
            a[i] %= 3;
        }
        for (int i = int(0); i < int(N); i++) {
            int k, t;
            cin >> k;
            for (int j = int(0); j < int(k); j++) {
                cin >> t;
                edges[t - 1].push_back(i);
            }
        }
        int ans = 1000000000;
        for (int i = int(0); i < int(3); i++) ans = min(ans, simulate(i));
        cout << ans + N << endl;
    }
    return 0;
}