[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]:
mod = 10^9+7
function fast_pow (get int a and b, return int)
    res=a, ret=1
    while b>0
        if b modulo 2 ret=(ret*res) modulo mod
        res=(res*res) modulo mod
        divide b by 2
    end of the block
    return ret
end of the block
n=int
m=int
c=array of 200 int
g=array of 200 by 200 int
define the main function
    read n
    for i=0 to n exclusive
        read c[i]
        decrement c[i]
    end of the block
    for i=0 to n exclusive
        k=int
        read k
        for j=0 to k exclusive
            u=int
            read u
            decrement u
            g[u][i]=1
        end of the block
    end of the block
    ans=100*n
    for start=0 to 3 exclusive
        m=0
        ok=array of 200 bool
        fill ok with 0
        cur=start
        res=n
        while m not equal n
            while true
                kpyto=false
                for i=0 to n exclusive
                    if ok[i] or c[i] != cur continue
                    win=true
                    for j=0 to n exclusive
                        if g[j][j] and not ok[j]
                            win=false
                            break
                        end of the block
                    end of the block
                    if win
                        kpyto=true
                        increment m
                        ok[i]=true
                    end of the block
                end of the block
                if not kpyto break
            end of the block
            if m is n break
            cur=(cur+1) modulo 3
            increment res
        end of the block
        ans=min(res, ans)
    end of the block
    print ans
    return to indicate successful execution
end of the block

[CODE]:

const int mod = 1e9 + 7;
int fast_pow(int a, int b) {
    int res = a, ret = 1;
    while (b > 0) {
        if (b % 2) ret = (ret * res) % mod;
        res = (res * res) % mod;
        b /= 2;
    }
    return ret;
}
int n;
int m;
int c[200];
int g[200][200];
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        c[i]--;
    }
    for (int i = 0; i < n; ++i) {
        int k;
        cin >> k;
        for (int j = 0; j < k; ++j) {
            int u;
            cin >> u;
            u--;
            g[u][i] = 1;
        }
    }
    int ans = 100 * n;
    for (int start = 0; start < 3; ++start) {
        m = 0;
        bool ok[200];
        memset(ok, 0, sizeof(ok));
        int cur = start;
        int res = n;
        while (m != n) {
            while (1) {
                bool kpyto = false;
                for (int i = 0; i < n; ++i) {
                    if (ok[i] || c[i] != cur) continue;
                    bool win = true;
                    for (int j = 0; j < n; ++j) {
                        if (g[j][i] && (!ok[j])) {
                            win = false;
                            break;
                        }
                    }
                    if (win) {
                        kpyto = true;
                        m++;
                        ok[i] = true;
                    }
                }
                if (!kpyto) break;
            }
            if (m == n) break;
            cur = (cur + 1) % 3;
            res++;
        }
        ans = min(res, ans);
    }
    cout << ans << endl;
    return 0;
}