#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  const int nax = 205;
  vector<int> a(nax);
  for (int i = 1; i <= n; i++) cin >> a[i];
  vector<int> v[nax];
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    while (k--) {
      int x;
      cin >> x;
      v[i].push_back(x);
    }
  }
  auto solution = [&](int x) {
    vector<int> deg(nax, 0);
    for (int i = 1; i <= n; i++) deg[i] = (int)v[i].size();
    vector<int> tmp[nax];
    for (int i = 1; i <= n; i++) {
      for (int y : v[i]) {
        tmp[y].push_back(i);
      }
    }
    int cnt = 0;
    int ans = 0;
    queue<int> can[5];
    for (int i = 1; i <= n; i++)
      if (deg[i] == 0) can[a[i]].push(i);
    while (cnt < n) {
      int T = 0;
      while (not can[x].empty()) {
        int first = can[x].front();
        can[x].pop();
        T++;
        for (int y : tmp[first]) {
          deg[y]--;
          if (deg[y] == 0) can[a[y]].push(y);
        }
      }
      ans += T;
      cnt += T;
      x = x == 3 ? 1 : x + 1;
      if (cnt < n) ans++;
    }
    return ans;
  };
  cout << min({solution(1), solution(2), solution(3)}) << '\n';
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}
