#include <bits/stdc++.h>
using namespace std;
const int Max = 500;
int n;
int com[3] = {1, 2, 3};
vector<int> g[Max];
int deg[Max], mp[Max], en[Max];
void input() {
  memset(deg, 0, sizeof(deg));
  memset(en, 0, sizeof(en));
  int a;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &mp[i]);
  }
  for (int u = 1; u <= n; u++) {
    scanf("%d", &a);
    for (int i = 0; i < a; i++) {
      int v;
      scanf("%d", &v);
      g[v].push_back(u);
      deg[u]++;
    }
  }
}
void dfs(int u, int a) {
  en[u] = 1;
  for (int j = 0; j < (int)g[u].size(); j++) {
    int v = g[u][j];
    if (--deg[v] == 0) {
      if (mp[v] == a) dfs(v, a);
    }
  }
}
int solve(int a) {
  memset(en, 0, sizeof(en));
  memset(deg, 0, sizeof(deg));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)g[i].size(); j++) {
      deg[g[i][j]]++;
    }
  }
  int sum = 0;
  int flag = 0;
  while (!flag) {
    for (int i = 1; i <= n; i++) {
      if (en[i] == 0) flag = 0;
      if (en[i] == 0 && deg[i] == 0 && mp[i] == com[a]) {
        dfs(i, com[a]);
      }
    }
    flag = 1;
    for (int i = 1; i <= n; i++)
      if (en[i] == 0) flag = 0;
    if (flag) break;
    a = (a + 1) % 3;
    sum++;
  }
  return n + sum;
}
int main() {
  cin >> n;
  input();
  int ans = 1000000000;
  for (int i = 0; i < 3; i++) {
    ans = min(ans, solve(i));
  }
  cout << ans << endl;
  return 0;
}
