#include <bits/stdc++.h>
using namespace std;
int dep[0x100][0x100];
int v[0x100];
int use[3];
int bad[0x100];
int cost[3][3] = {{1, 2, 3}, {3, 1, 2}, {2, 3, 1}};
int main() {
  int N, x;
  memset(bad, 0, sizeof(bad));
  memset(dep, 0, sizeof(dep));
  memset(use, 0, sizeof(use));
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> v[i];
    --v[i];
  }
  for (int i = 1; i <= N; ++i) {
    cin >> dep[0][i];
    if (!dep[0][i]) use[v[i]] = 1;
    for (int j = 1; j <= dep[0][i]; ++j) {
      cin >> x;
      dep[i][x] = 1;
    }
  }
  int timp = 0x7FFFFFFF;
  for (int i = 0; i < 3; ++i)
    if (use[i]) {
      int num = i, next;
      int t = 0;
      memmove(bad, dep[0], sizeof(dep[0]));
      for (int i = 1; i <= N; ++i) {
        int min = 0x7FFFFFFF;
        for (int j = 1; j <= N; ++j)
          if (cost[num][v[j]] < min && !bad[j]) min = cost[num][v[j]], next = j;
        --bad[next];
        for (int j = 1; j <= N; ++j)
          if (dep[j][next]) --bad[j];
        t += min;
        num = v[next];
      }
      if (timp > t) timp = t;
    }
  cout << timp << '\n';
  return 0;
}
