#include <bits/stdc++.h>
using namespace std;
int n, c[1007], k[1007], a, ck[1007], ans1, ans2, ans3, vis[1007];
vector<int> edge[1007], par[1007];
queue<int> pr, pl;
int check() {
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) return 0;
  }
  return 1;
}
int col(int x) {
  for (int i = 0; i < par[x].size(); i++) {
    int j = par[x][i];
    if (vis[j] == 0) return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> k[i];
    for (int j = 1; j <= k[i]; j++) {
      cin >> a;
      edge[a].push_back(i);
      par[i].push_back(a);
    }
  }
  ans1 = n;
  ans2 = n;
  ans3 = n;
  for (int i = 1; i <= n; i++) {
    if (k[i] == 0) pr.push(i);
  }
  int cur = 1;
  while (check() == 0) {
    memset(ck, 0, sizeof(ck));
    int ok = 0;
    if (cur == 4) cur = 1;
    while (!pr.empty()) {
      int cl = pr.front();
      pr.pop();
      if (c[cl] == cur)
        vis[cl] = 1;
      else
        pl.push(cl);
      for (int i = 0; i < edge[cl].size(); i++) {
        int kl = edge[cl][i];
        if (ck[kl] == 0) {
          ck[kl] = 1;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (ck[i] == 1 && col(i) == 1) {
        pl.push(i);
        ok = 1;
      }
    }
    while (!pl.empty()) {
      pr.push(pl.front());
      pl.pop();
    }
    if (ok == 0) {
      cur++;
      ans1++;
    }
  }
  while (!pr.empty()) pr.pop();
  for (int i = 1; i <= n; i++) {
    if (k[i] == 0) pr.push(i);
  }
  cur = 2;
  memset(vis, 0, sizeof(vis));
  while (check() == 0) {
    memset(ck, 0, sizeof(ck));
    int ok = 0;
    if (cur == 4) cur = 1;
    while (!pr.empty()) {
      int cl = pr.front();
      pr.pop();
      if (c[cl] == cur)
        vis[cl] = 1;
      else
        pl.push(cl);
      for (int i = 0; i < edge[cl].size(); i++) {
        int kl = edge[cl][i];
        if (ck[kl] == 0) {
          ck[kl] = 1;
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (ck[i] == 1 && col(i) == 1) {
        pl.push(i);
        ok = 1;
      }
    }
    while (!pl.empty()) {
      pr.push(pl.front());
      pl.pop();
    }
    if (ok == 0) {
      cur++;
      ans2++;
    }
  }
  while (!pr.empty()) pr.pop();
  for (int i = 1; i <= n; i++) {
    if (k[i] == 0) pr.push(i);
  }
  cur = 3;
  memset(vis, 0, sizeof(vis));
  while (check() == 0) {
    memset(ck, 0, sizeof(ck));
    int ok = 0;
    if (cur == 4) cur = 1;
    while (!pr.empty()) {
      int cl = pr.front();
      pr.pop();
      if (c[cl] == cur)
        vis[cl] = 1;
      else
        pl.push(cl);
      for (int i = 0; i < edge[cl].size(); i++) {
        int kl = edge[cl][i];
        ck[kl] = 1;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (ck[i] == 1 && col(i) == 1) {
        pl.push(i);
        ok = 1;
      }
    }
    while (!pl.empty()) {
      pr.push(pl.front());
      pl.pop();
    }
    if (ok == 0) {
      cur++;
      ans3++;
    }
  }
  int ans = ans1;
  ans = min(ans, ans2);
  ans = min(ans, ans3);
  cout << ans - 1;
}
