int n;
int a[400005];
long long aux[22][22];
long long sump[22];
long long dp[(1<<20) + 5];
int main() {
cin>>n;
for (int i=1; i<=n; ++i) {
cin>>a[i];
++sump[a[i]];
for (int j=1; j<=20; ++j)
if (j!=a[i]) aux[a[i]][j]+=sump[j];
}
for (int i=1; i<(1<<20); ++i) {
dp[i]=(long long)1000000000 * (long long)1000000000;
for (int j=0; j<20; ++j)
if (i&(1<<j)) {
long long dpaux = 0;
for (int k=0; k<20; ++k)
if (k!=j && (i&(1<<k))) dpaux+=aux[j+1][k+1];
dp[i]=min(dp[i],dpaux+dp[i-(1<<j)]);
}
}
cout<<dp[(1<<20)-1];
return 0;
}