#include <bits/stdc++.h>
using namespace std;
using ld = double;
using ll = long long;
const int INF = 1e9, MOD = 1e9 + 7;
void solve();
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int q = 1;
while (q--) {
solve();
}
}
const int MAXC = 21;
int add(int mask, int x) {
for (int c = x; c >= 0; c--) {
if ((mask >> c) & 1) {
mask -= (1 << c);
break;
}
}
mask += (1 << x);
return mask;
}
void solve() {
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i]--;
}
vector<vector<int>> hv(MAXC * MAXC);
for (int i = 1; i + 1 <= n; i++) {
hv[a[i] * MAXC + a[i + 1]].push_back(i);
}
int B = MAXC * MAXC;
vector<vector<int>> nxt_blk(MAXC * MAXC, vector<int>(n / B + 1, INF));
for (int id = 0; id < MAXC * MAXC; id++) {
int ptr = 0;
for (int k = 1; k <= n / B; k++) {
int pos = k * B;
while (ptr < (int)hv[id].size() && hv[id][ptr] < pos) {
ptr++;
}
if (ptr < (int)hv[id].size()) {
nxt_blk[id][k] = hv[id][ptr];
}
}
}
vector<int> dp(1 << MAXC, -INF);
dp[0] = 0;
for (int mask = 0; mask < (1 << MAXC); mask++) {
if (dp[mask] == -INF) continue;
int brk[MAXC];
int brk_sz = 0;
int in_brk_mask = 0;
for (int c = 0; c < MAXC; c++) {
if (mask != add(mask, c)) {
brk[brk_sz++] = c;
in_brk_mask |= (1 << c);
}
}
int cls = INF;
int j = dp[mask] + 1;
bool found = false;
while (j <= n && (j % B) != 0) {
if (j + 1 <= n && ((in_brk_mask >> a[j]) & 1) && ((in_brk_mask >> a[j + 1]) & 1)) {
cls = j - 1;
found = true;
break;
}
j++;
}
if (!found && j <= n) {
int k = j / B;
for (int xi = 0; xi < brk_sz; xi++) {
for (int yi = 0; yi < brk_sz; yi++) {
int id = brk[xi] * MAXC + brk[yi];
int pos = nxt_blk[id][k];
if (pos != INF) {
cls = min(cls, pos - 1);
}
}
}
}
if (cls == INF) {
if (mask != add(mask, a[n])) cls = n - 1;
else cls = n;
}
dp[mask] = cls;
if (dp[mask] + 1 > n) continue;
int mask1 = add(mask, a[dp[mask] + 1]);
dp[mask1] = max(dp[mask1], dp[mask] + 1);
if (dp[mask] + 2 > n) continue;
int mask2 = add(mask, a[dp[mask] + 2]);
dp[mask2] = max(dp[mask2], dp[mask] + 2);
}
int ans = INF;
for (int mask = 0; mask < (1 << MAXC); mask++) {
if (dp[mask] == n || dp[mask] == n - 1) {
int cnt = __builtin_popcount(mask);
ans = min(ans, cnt);
}
}
cout << ans << "\n";
}