#include <bits/stdc++.h>
using namespace std;
long long cur, allmask;
long long a[400001];
long long to[20][20], cnt[20 + 1];
long long dp[(1 << 20)];
long long solve(long long mask) {
  if (mask == allmask) return 0;
  long long &res = dp[mask];
  if (res != -1) return res;
  res = 1e14;
  long long idx = __builtin_popcount(mask);
  for (long long i = 0; i < cur; i++) {
    if (mask & (1 << i)) continue;
    long long cur_mask = (mask | (1 << i)), ans = 0;
    for (long long j = 0; j < cur; j++) {
      if (cur_mask & (1 << j)) continue;
      ans += to[i][j];
    }
    res = min(res, ans + solve(cur_mask));
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  cur = 0;
  for (long long i = 1; i <= 20; i++)
    if (cnt[i] > 0) cnt[i] = cur++;
  for (long long i = 0; i < n; i++) a[i] = cnt[a[i]];
  for (long long i = 0; i < cur; i++) {
    for (long long j = 0; j < cur; j++) {
      if (i == j) continue;
      long long cntj = 0;
      for (long long ii = 0; ii < n; ii++) {
        if (a[ii] == j)
          cntj++;
        else if (a[ii] == i)
          to[i][j] += cntj;
      }
    }
  }
  memset(dp, -1, sizeof(dp));
  allmask = (1ll << cur) - 1;
  long long ans = solve(0);
  cout << ans << "\n";
}
