#include <bits/stdc++.h>
using namespace std;
int child[205];
int k[205][205];
int c[205];
vector<int> parent[205];
int cost(int now, int n) {
  vector<int> todo[5];
  for (int i = 0; i < n; i++) {
    if (k[i][0] == 0) todo[c[i]].push_back(i);
    child[i] = k[i][0];
  }
  int ans = 0;
  bool first = true;
  while (todo[0].size() + todo[1].size() + todo[2].size()) {
    if (!first) ans++;
    first = false;
    while (todo[now].size()) {
      vector<int> New;
      while (todo[now].size()) {
        int i = todo[now].size() - 1;
        for (int j = 0; j < parent[todo[now][i]].size(); j++) {
          child[parent[todo[now][i]][j]]--;
          if (child[parent[todo[now][i]][j]] == 0)
            New.push_back(parent[todo[now][i]][j]);
        }
        todo[now].pop_back();
      }
      for (int i = New.size() - 1; i >= 0; i--) {
        todo[c[New[i]]].push_back(New[i]);
        New.pop_back();
      }
    }
    now = (now + 1) % 3;
  }
  return ans;
}
int main() {
  int n, t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  for (int i = 0; i < n; i++) {
    cin >> k[i][0];
    for (int j = 0; j < k[i][0]; j++) {
      cin >> t;
      t--;
      parent[t].push_back(i);
    }
  }
  cout << min(min(cost(0, n), cost(1, n)), cost(2, n)) + n << endl;
  return 0;
}
