#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
int n, k, a[N], b[N], sum[22];
long long dp[1050000], cnt[22][22];
inline void Do(long long &x, long long y) {
  if ((~x && x > y) || x == -1) x = y;
}
int main() {
  scanf("%d", &n);
  register int i, j, o;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + n + 1);
  k = unique(b + 1, b + n + 1) - b - 1;
  for (i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + k + 1, a[i]) - b - 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < k; j++) cnt[j][a[i]] += sum[j];
    sum[a[i]]++;
  }
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (i = 0; i < (1 << k); i++)
    if (~dp[i]) {
      for (j = 0; j < k; j++) {
        int sta = 1 << j;
        if (sta & i) continue;
        long long all = 0;
        for (o = 0; o < k; o++)
          if ((1 << o) & i) all += cnt[j][o];
        Do(dp[i | sta], dp[i] + all);
      }
    }
  printf("%lld\n", dp[(1 << k) - 1]);
}
