#include <bits/stdc++.h>
long long q_pow(long long x, long long n, long long mod) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = x * ret % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
long long __gcd(long long x, long long y) {
  if (!y) return x;
  return __gcd(y, x % y);
}
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
using namespace std;
const long long INF = 0x3f3f3f3f;
int mod = 1e9 + 7;
const int maxn = 4e5 + 5;
long long dp[(1 << 20) + 50];
long long val[25][25];
long long cnt[maxn];
signed main(int argc, char *argv[]) {
  if (argc == 2 && strcmp("-debug", argv[1]) == 0) {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  }
  int n;
  int tmp;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &tmp);
    tmp--;
    cnt[tmp]++;
    for (int j = 0; j < 20; j++) val[j][tmp] += cnt[j];
  }
  int all = (1 << 20) - 1;
  for (int i = 1; i <= all; i++) {
    dp[i] = INF * INF;
    for (int j = 0; j < 20; j++) {
      int now = i ^ (1 << j);
      if (((1 << j) & i) == 0) continue;
      long long sum = 0;
      for (int k = 0; k < 20; k++) {
        if (k == j) continue;
        if (((1 << k) & i) == 0) continue;
        sum += val[k][j];
      }
      dp[i] = min(dp[i], dp[now] + sum);
    }
  }
  cout << dp[all] << endl;
}
