#include <bits/stdc++.h>
using namespace std;
int comp[205];
int depend[205][205];
int coun[205] = {0};
int temp[205] = {0};
vector<int> need[205];
int ans;
int n, i, j, k, x;
void func(int m, int z) {
  coun[z] = -1;
  ans++;
  for (int x = 0; x < need[z].size(); x++) {
    coun[need[z][x]]--;
    if (comp[need[z][x]] == m && coun[need[z][x]] == 0) {
      func(m, need[z][x]);
    }
  }
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> comp[i];
  for (i = 1; i <= n; i++) {
    int z;
    cin >> z;
    temp[i] = z;
    for (j = 0; j < z; j++) {
      cin >> depend[i][j];
      need[depend[i][j]].push_back(i);
    }
  }
  int minn;
  for (i = 1; i <= 3; i++) {
    j = i;
    ans = 0;
    for (k = 1; k <= n; k++) {
      coun[k] = temp[k];
    }
    while (1) {
      for (k = 1; k <= n; k++) {
        if (comp[k] == j && coun[k] == 0) {
          func(j, k);
        }
      }
      j = j + 1;
      if (j == 4) {
        j = 1;
      }
      int ctr = 0;
      for (x = 1; x <= n; x++) {
        if (coun[x] == -1) ctr++;
      }
      if (ctr == n) {
        if (i == 1)
          minn = ans;
        else
          minn = min(minn, ans);
        break;
      }
      ans++;
    }
  }
  cout << minn;
  return 0;
}
