#include <bits/stdc++.h>
using namespace std;
int n, A[400005];
int S[25][400005];
long long f[1 << 20], T[25][25], all[25];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    S[A[i]][i]++;
    for (int j = 1; j <= 20; j++) {
      S[j][i] += S[j][i - 1];
      T[A[i]][j] += S[j][i];
    }
    all[A[i]] += i - S[A[i]][i];
  }
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  for (int i = 0; i < (1 << 20); i++) {
    for (int k = 0; k < 20; k++) {
      if (i & (1 << k)) continue;
      long long cst = all[k + 1];
      for (int l = 0; l < 20; l++)
        if (i & (1 << l)) cst -= T[k + 1][l + 1];
      f[i ^ (1 << k)] = min(f[i ^ (1 << k)], f[i] + cst);
    }
  }
  printf("%I64d\n", f[(1 << 20) - 1]);
}
