[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]:
define the main function
    n,m,a=int
    read n
    c and n = array of n+1 int, filled with 0
    g=array of n+2 vector of int
    cc=array of 5 vector of int
    for i=0 to n exclusive
        read c[i]
        decrement c[i]
        add i to end of cc[c[i]]
    end of the block
    for i=0 to n exclusive
        read m
        while decremented value of m is not 0
            read a
            add i to end of g[a-1]
            increment d[i]
        end of the block
    end of the block
    mx=INT_MAX
    for k=0 to n exclusive
        if d[k] is 0
            dp=array of n+1 int
            for i=0 to n exclusive dp[i]=d[i]
            p=c[k], ok=0, an=n
            while true
                if ok is n break
                for i=0 to size of cc[p] exclusive
                    if d[cc[p][i]] is 0
                        decrement d[cc[p][i]]
                        increment ok
                        for j=0 to size of g[cc[p][i]] exclusive
                            u=g[cc[p][i]][j]
                            decrement d[u]
                        end of the block
                        i=-1
                    end of the block
                end of the block
                if ok and ok<n increment an
                p=(p+1) modulo 3
            end of the block
            for i=0 to n exclusive d[i]=dp[i]
            mx=min(mx,an)
        end of the block
    print mx
end of the block

[CODE]:

int main() {
    int n, m, a;
    cin >> n;
    int c[n + 1], d[n + 1] = {0};
    vector<int> g[n + 2];
    vector<int> cc[5];
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        c[i]--;
        cc[c[i]].push_back(i);
    }
    for (int i = 0; i < n; i++) {
        cin >> m;
        while (m--) {
            cin >> a;
            g[a - 1].push_back(i);
            d[i]++;
        }
    }
    int mx = INT_MAX;
    for (int k = 0; k < n; k++)
        if (d[k] == 0) {
            int dp[n + 1];
            for (int i = 0; i < n; i++) dp[i] = d[i];
            int p = c[k], ok = 0, an = n;
            while (1) {
                if (ok == n) break;
                for (int i = 0; i < cc[p].size(); i++) {
                    if (d[cc[p][i]] == 0) {
                        d[cc[p][i]]--;
                        ok++;
                        for (int j = 0; j < g[cc[p][i]].size(); j++) {
                            int u = g[cc[p][i]][j];
                            d[u]--;
                        }
                        i = -1;
                    }
                }
                if (ok && ok < n) an++;
                p = (p + 1) % 3;
            }
            for (int i = 0; i < n; i++) d[i] = dp[i];
            mx = min(mx, an);
        }
    cout << mx << endl;
}