#include <bits/stdc++.h>
using namespace std;
int n;
long long s[32][32];
int a[400010];
const int M = 20;
long long f[1 << M];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= M; i++)
    for (int j = 1; j <= M; j++) {
      if (i == j) continue;
      int cnt = 0;
      for (int k = 1; k <= n; k++)
        if (a[k] == i)
          cnt++;
        else if (a[k] == j)
          s[i][j] += cnt;
    }
  memset(f, 0x7f, sizeof(f));
  f[0] = 0;
  for (int i = 0; i < (1 << M); i++)
    for (int j = 1; j <= M; j++)
      if (!(i & (1 << (j - 1)))) {
        long long ret = 0;
        for (int k = 1; k <= M; k++)
          if (i & (1 << (k - 1))) ret += s[j][k];
        f[i | (1 << (j - 1))] = min(f[i | (1 << (j - 1))], f[i] + ret);
      }
  printf("%I64d\n", f[(1 << M) - 1]);
  return 0;
}
