#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000000;   const double pi = 2 * acos(0.0);   const int MOD = 1000000007;   vector<int> g[5];   int id[222][222], k[222];   bool ok[222];   int wh[222];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> wh[i];       g[wh[i]].push_back(i);     }     for (int i = 1; i <= n; ++i) {       cin >> k[i];       for (int j = 1; j <= k[i]; ++j) cin >> id[i][j];     }     int res = inf;     for (int ST = 1; ST <= 3; ++ST) {       int st = ST;       memset(ok, false, sizeof(ok));       int cnt = 0, ans = 0;       for (;;) {         if (cnt == n) break;         for (;;) {           bool doing = false;           for (int c = 0; c <= (int)g[st].size() - 1; ++c) {             int work = g[st][c];             if (ok[work]) continue;             bool is = true;             for (int i = 1; i <= k[work]; ++i)               if (!ok[id[work][i]]) is = false;             if (is) {               doing = true;               ++ans;               ok[work] = true;               ++cnt;             }           }           if (!doing) break;         }         if (cnt == n) break;         ++ans;         st++;         if (st == 4) st = 1;       }       res = min(res, ans);     }     cout << res << endl;   }   