#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int inf = INT_MAX;
const long long mod = 1e9 + 7;
const int pi = acos(-1);
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 505;
vector<int> g[N];
int c[N], sz[N], d[N], ch[4][4];
bool used[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ch[i][j] = 1;
    }
  }
  ch[1][2] = 1;
  ch[2][3] = 1;
  ch[3][1] = 1;
  ch[1][3] = 2;
  ch[3][2] = 2;
  ch[2][1] = 2;
  for (int i = 1; i <= n; i++) {
    cin >> sz[i];
    for (int j = 1; j <= sz[i]; j++) {
      int a;
      cin >> a;
      g[a].push_back(i);
    }
  }
  int ans = inf;
  for (int i = 1; i <= 3; i++) {
    int s = 0;
    for (int j = 1; j <= n; j++) {
      used[j] = 0;
    }
    for (int j = 1; j <= n; j++) {
      if (c[j] == i && sz[j] == 0) {
        s = j;
        break;
      }
    }
    if (s == 0) {
      continue;
    }
    for (int j = 1; j <= n; j++) {
      d[j] = sz[j];
    }
    priority_queue<pair<int, int> > q;
    q.push(make_pair(i, s));
    used[s] = 1;
    int res = 0;
    int cur = i;
    while (!q.empty()) {
      int v = q.top().second;
      int val = q.top().first;
      if (cur != val) res += ch[cur][val], cur = val;
      q.pop();
      for (int j = 0; j < g[v].size(); j++) {
        int to = g[v][j];
        d[to]--;
      }
      bool w = 0;
      for (int j = 1; j <= n; j++) {
        if (!used[j] && d[j] == 0 && c[j] == cur) {
          q.push(make_pair(c[j], j));
          used[j] = 1;
          w = 1;
          break;
        }
      }
      if (!w) {
        int p = cur + 1;
        if (p == 4) p = 1;
        bool u = 0;
        for (int j = 1; j <= n; j++) {
          if (!used[j] && d[j] == 0 && c[j] == p) {
            q.push(make_pair(c[j], j));
            u = 1;
            used[j] = 1;
            break;
          }
        }
        if (!u) {
          p++;
          if (p == 4) p = 1;
          for (int j = 1; j <= n; j++) {
            if (!used[j] && d[j] == 0 && c[j] == p) {
              q.push(make_pair(c[j], j));
              used[j] = 1;
              break;
            }
          }
        }
      }
    }
    ans = min(ans, res);
  }
  cout << ans + n;
}
