#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << "\n";
  err(++it, args...);
}
const long long M = 20, inf = 1e18;
long long c[M][M], cnt[M];
vector<long long> dp(1 << M, inf);
void mins(long long &x, long long y) { x = min(x, y); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  };
  dp[0] = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < M; j++) {
      long long cnt = 0, cost = 0;
      for (int k = 0; k < n; k++) {
        cnt += a[k] == j;
        if (a[k] == i) {
          cost += cnt;
        }
      }
      c[i][j] = cost;
    }
    cnt[i] = count(a, a + n, i);
  }
  for (int msk = 0; msk < 1 << M; msk++) {
    for (int i = 0; i < M; i++)
      if (not(msk >> i & 1)) {
        long long cost = 0;
        int nmsk = msk | (1 << i);
        for (int j = 0; j < M; j++)
          if (not(msk >> j & 1)) {
            cost += c[i][j];
          }
        cost -= cnt[i] * (cnt[i] + 1) / 2;
        mins(dp[nmsk], dp[msk] + cost);
      }
  }
  cout << dp[(1 << M) - 1] << "\n";
}
