#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10, mx = 1 << 20;
long long s[30], cnt[30][30], dp[N];
int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &x);
    for (int j = 1; j <= 20; ++j) cnt[x][j] += s[j];
    s[x]++;
  }
  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  for (int i = 0; i < mx - 1; ++i)
    for (int j = 1; j <= 20; ++j)
      if (!((i >> (j - 1) & 1))) {
        long long tmp = 0;
        for (int k = 1; k <= 20; ++k)
          if ((i >> (k - 1)) & 1) tmp += cnt[k][j];
        dp[i ^ (1 << (j - 1))] = min(dp[i ^ (1 << (j - 1))], dp[i] + tmp);
      }
  printf("%lld\n", dp[mx - 1]);
}
