#include <bits/stdc++.h>
using namespace std;
struct IO {
  IO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  }
} _;
int n, m, len, k, t, a, b;
set<int> adj[201];
set<int> radj[201];
set<int> adjB[201];
set<int> radjB[201];
vector<int> parts[3];
int arr[201];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i], arr[i]--, parts[arr[i]].push_back(i);
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> a;
      a--;
      adj[a].insert(i);
      radj[i].insert(a);
    }
  }
  for (int i = 0; i < n; i++) {
    adjB[i] = set<int>(adj[i].begin(), adj[i].end());
    radjB[i] = set<int>(radj[i].begin(), radj[i].end());
  }
  int anses = 1000000007;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      adj[j] = set<int>(adjB[j].begin(), adjB[j].end());
      radj[j] = set<int>(radjB[j].begin(), radjB[j].end());
    }
    queue<int> q[3];
    for (int j = 0; j < n; j++)
      if (((int)radj[j].size()) == 0) q[arr[j]].push(j);
    int cur = i;
    int ans = 0;
    while (!q[0].empty() || !q[1].empty() || !q[2].empty()) {
      if (q[cur].empty()) {
        ans++;
        cur++;
        cur %= 3;
        continue;
      }
      int u = q[cur].front();
      q[cur].pop();
      ans++;
      for (auto v : adj[u]) {
        radj[v].erase(u);
        if (((int)radj[v].size()) == 0) q[arr[v]].push(v);
      }
      adj[u].clear();
    }
    anses = min(anses, ans);
  }
  cout << anses << "\n";
}
