#include <bits/stdc++.h>
using namespace std;
bool cmp1(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
void showvd(vector<double> v) {
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) cout << v[i] << " " << endl;
}
void showvi(vector<int> v) {
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
void showvl(vector<long long> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << " "
         << " ";
  cout << endl;
}
void showvp(vector<pair<int, int> > v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i].first << " " << v[i].second << endl;
}
long long fastpower(long long a, long long n, long long p) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) ans = (ans * a) % p;
    a = (a * a) % p;
    n = n >> 1;
  }
  return ans % p;
}
vector<bool> prime(100000, true);
void SOE(int n) {
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
}
void factor(int n, vector<int> &v) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      v.push_back(i);
      int z = n / i;
      if (z != i) v.push_back(z);
    }
  }
}
int cmp[202];
int gr[202][202];
int chk(int cur, int n) {
  vector<int> dep(n, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (gr[j][i] == 1) dep[i]++;
    }
  int tmp = n, ans = 0;
  vector<int> mark(n, 0);
  while (tmp != 0) {
    int id = -1;
    for (int i = 0; i < n; i++) {
      if (!mark[i] && cmp[i] == cur && dep[i] == 0) {
        id = i;
        break;
      }
    }
    if (id == -1) {
      cur = (cur + 1) % 3;
      ans++;
    } else {
      ans++;
      mark[id] = 1;
      for (int i = 0; i < n; i++) {
        if (gr[id][i] == 1) dep[i]--;
      }
      tmp--;
    }
  }
  return ans;
}
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    cmp[i] = x - 1;
  }
  for (int i = 0; i < 202; i++)
    for (int j = 0; j < 202; j++) gr[i][j] = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int y;
      cin >> y;
      gr[y - 1][i] = 1;
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < 3; i++) ans = min(ans, chk(i, n));
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  for (int i = 0; i < t; i++) {
    solve();
  }
  return 0;
}
