#include <bits/stdc++.h>
using namespace std;
int d[10005];
int d2[10005];
int pos[10005];
int cost[4][4] = {{0, 0, 0, 0}, {0, 0, 1, 2}, {0, 2, 0, 1}, {0, 1, 2, 0}};
vector<int> e[10005];
bool book[10005];
int main(void) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", pos + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", d + i);
    for (int j = 1; j <= d[i]; j++) {
      int a;
      scanf("%d", &a);
      e[a].push_back(i);
    }
  }
  int an = 1e6;
  for (int tt = 1; tt <= 3; tt++) {
    int t = tt;
    for (int i = 1; i <= n; i++) d2[i] = d[i];
    int co = 0;
    list<int> v;
    for (int i = 1; i <= n; i++) {
      if (d2[i] == 0) {
        v.push_back(i);
      }
    }
    while (!v.empty()) {
      list<int>::iterator a = v.begin();
      for (auto it = v.begin(); it != v.end(); it++) {
        if (cost[t][pos[*a]] > cost[t][pos[*it]]) {
          a = it;
        }
      }
      co += cost[t][pos[*a]];
      t = pos[*a];
      int aa = *a;
      v.erase(a);
      for (auto x : e[aa]) {
        d2[x]--;
        if (d2[x] == 0) v.push_back(x);
      }
    }
    an = min(an, co);
  }
  cout << an + n << endl;
  return 0;
}
