#include <bits/stdc++.h>
using namespace std;
int n, a, siz[21];
const long long inf = 1e11;
const int cnt = 20;
long long dp[1048576], w[21][21];
int main() {
  scanf("%d", &n);
  for (register int i = 1; i <= n; ++i) {
    scanf("%d", &a);
    ++siz[a];
    for (register int j = 1; j <= cnt; ++j) w[j][a] += siz[j];
  }
  for (register int i = 1; i < (1 << cnt); ++i) {
    dp[i] = inf;
    for (register int j = 1; j <= cnt; ++j)
      if (i & (1 << (j - 1))) {
        int zt = i ^ (1 << (j - 1));
        long long val = 0;
        for (register int k = 1; k <= cnt; ++k)
          if (zt & (1 << (k - 1))) val += w[k][j];
        dp[i] = min(dp[i], dp[zt] + val);
      }
  }
  printf("%lld\n", dp[(1 << cnt) - 1]);
  return 0;
}
