#include <bits/stdc++.h>
using namespace std;
long min(long int a, long int b) { return (a) > (b) ? (b) : (a); }
bool g[210][210] = {}, vis[210] = {};
long c[210] = {}, cnt[210] = {};
long n, m, k, i, j, l, r, b, d, t, u, w, x, y, z, sum = 0, ans = 0;
long func(long p) {
  int t = n, ans = 0;
  for (i = 0; i < n; i++) {
    cnt[i] = 0;
    vis[i] = false;
    for (j = 0; j < n; j++) {
      if (g[j][i]) cnt[i]++;
    }
  }
  while (t--) {
    bool found = false;
    found = 0;
    for (i = 0; i < n; i++) {
      if (!vis[i] && c[i] == p && !cnt[i]) {
        found = true;
        break;
      }
    }
    if (!found) {
      ans++, t++, p = (p + 1) % 3;
      continue;
    }
    vis[i] = true;
    for (j = 0; j < n; j++) {
      if (g[i][j]) cnt[j]--;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
  }
  for (i = 0; i < n; i++) {
    cin >> x;
    for (j = 1; j <= x; j++) {
      cin >> y;
      g[y - 1][i] = true;
    }
  }
  ans = 100000000;
  for (int i = 0; i < 3; i++) {
    ans = min(ans, func(i));
  }
  cout << ans + n;
  return 0;
}
