#include <bits/stdc++.h>
using namespace std;
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
const long long int MAXN = 4e5 + 10;
const long long int MAXM = (1 << 20);
long long int a[MAXN];
long long int count_ahead[20][20];
long long int dp[MAXM];
long long int n;
long long int num = 0;
long long int rec(long long int mask) {
  if (mask == MAXM - 1) return 0;
  long long int &ans = dp[mask];
  if (ans != -1) return ans;
  ans = n * n * n;
  for (long long int i = 0; i <= 19; i++) {
    if ((mask & (1 << i)) == 0) {
      long long int countt = 0;
      for (long long int j = 0; j <= 19; j++) {
        if ((mask & (1 << j))) {
          countt += count_ahead[i][j];
        }
      }
      ans = min(ans, rec(mask | (1 << i)) + countt);
    }
  }
  return ans;
}
void solve() {
  memset((dp), -1, sizeof((dp)));
  cin >> n;
  for (long long int i = (1); i <= (n); ++i) {
    cin >> a[i];
    a[i]--;
  }
  for (long long int i = (0); i <= (19); ++i) {
    for (long long int j = (0); j <= (19); ++j) {
      long long int temp = 0;
      for (long long int k = (n); k >= (1); --k) {
        if (a[k] == j) temp++;
        if (a[k] == i) {
          count_ahead[i][j] += temp;
        }
      }
    }
  }
  cout << rec(0) << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int t;
  t = 1;
  for (long long int i = (1); i <= (t); ++i) {
    solve();
  }
}
