#include <bits/stdc++.h>
using namespace std;
long long int input(long long int n);
long long int input(long long int n, vector<long long int> &ve) {
  long long int val;
  for (long long int i = 0; i < n; i++) {
    cin >> val;
    ve.push_back(val);
  }
  return 0;
}
vector<long long int> one, two, three;
vector<vector<long long int>> v;
long long int n;
long long int fun(long long int a, long long int cost,
                  vector<long long int> &arr) {
  long long int r = n, f = 1;
  for (long long int i = 0; i < n; i++) {
    if (arr[i] == 0) {
      f = 0;
    }
  }
  if (f == 1) {
    return cost - 1;
  }
  vector<long long int> ve;
  if (a == 1) {
    ve = one;
  } else if (a == 2) {
    ve = two;
  } else {
    ve = three;
  }
  while (r--) {
    for (long long int i = 0; i < ve.size(); i++) {
      long long int t = ve[i], flag = 1;
      if (arr[t] == 0) {
        for (long long int j = 0; j < v[t].size(); j++) {
          if (arr[v[t][j]] == 0) {
            flag = 0;
          }
        }
        if (flag == 1) {
          arr[t] = 1;
        }
      }
    }
  }
  ++cost;
  a++;
  if (a == 4) {
    a = 1;
  }
  return fun(a, cost, arr);
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int a;
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> a;
    if (a == 1) {
      one.push_back(i);
    } else if (a == 2) {
      two.push_back(i);
    } else {
      three.push_back(i);
    }
  }
  long long int b;
  v.resize(n + 1);
  for (long long int i = 0; i < n; i++) {
    cin >> a;
    for (long long int j = 0; j < a; j++) {
      cin >> b;
      v[i].push_back(--b);
    }
  }
  long long int c = 1;
  vector<long long int> arr(n + 1, 0);
  long long int q, w, e;
  q = fun(1, 0, arr);
  for (long long int i = 0; i < n + 1; i++) {
    arr[i] = 0;
  }
  w = fun(2, 0, arr);
  for (long long int i = 0; i < n + 1; i++) {
    arr[i] = 0;
  }
  e = fun(3, 0, arr);
  if (q <= w && q <= e) {
    cout << q + n << endl;
  } else if (w <= q && w <= e) {
    cout << w + n << endl;
  } else {
    cout << e + n << endl;
  }
  return 0;
  ;
}
