#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
const int NMAX = 205;
int n, cnt[NMAX];
vector<int> dep[NMAX], ped[NMAX], c[NMAX];
int r[NMAX], order = 0;
bool marked[NMAX];
void dfs(int u) {
  marked[u] = true;
  for (auto v : dep[u]) {
    if (!marked[v]) {
      dfs(v);
    }
  }
  r[u] = ++order;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int color;
    cin >> color;
    c[color].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    while (k--) {
      int j;
      cin >> j;
      dep[i].push_back(j);
      ped[j].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!marked[i]) dfs(i);
  }
  for (int i = 1; i <= 3; i++) {
    sort(c[i].begin(), c[i].end(), [](int u, int v) { return r[u] < r[v]; });
  }
  int mn = INT_MAX;
  for (int i = 1; i <= 3; i++) {
    int cnt[NMAX];
    int o[NMAX];
    o[1] = c[1].size(), o[2] = c[2].size(), o[3] = c[3].size();
    for (int j = 1; j <= n; j++) {
      cnt[j] = dep[j].size();
    }
    int res = 0;
    int cur = i;
    while (true) {
      for (auto p : c[cur]) {
        if (cnt[p] == 0) {
          o[cur]--;
          cnt[p] = -1;
          for (auto j : ped[p]) cnt[j]--;
        }
      }
      if (o[1] || o[2] || o[3]) {
        res++;
        cur = cur + 1;
        if (cur == 4) cur = 1;
      } else {
        break;
      }
    }
    mn = min(mn, res);
  }
  cout << mn + n;
}
