#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char ch = getchar();
  long long f = 1, x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
long long a[400001], ma[21][21], dp[1048576], now[25];
signed main() {
  long long n = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i < 21; ++i)
    for (int j = 1; j < 21; ++j)
      if (i != j) {
        int tot = 0;
        for (int k = n; k; --k) {
          if (a[k] == i) tot++;
          if (a[k] == j) ma[i][j] += tot;
        }
      }
  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  for (int i = 0; i < 1048576; ++i) {
    memset(now, 0, sizeof now);
    for (int j = 1; j <= 20; ++j)
      if (i & (1 << j - 1))
        for (int k = 1; k <= 20; ++k) now[k] += ma[j][k];
    for (int j = 1; j <= 20; ++j)
      if (!(i & (1 << j - 1)))
        dp[i | (1 << j - 1)] = min(dp[i | (1 << j - 1)], dp[i] + now[j]);
  }
  printf("%I64d\n", dp[1048575]);
  return 0;
}
