#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
const ll MOD = 998244353;
const ll INF = 1e18 + 9;
const int MAXN = 1000006;
const int MAXC = 20;
int n, arr[MAXN], num[MAXC];
ll dist[MAXC][MAXC], mem[1 << MAXC], realDist[1 << MAXC][MAXC];
ll solve(int p) {
  if (mem[p] != -1) return mem[p];
  mem[p] = INF;
  for (int i = 0; i < MAXC; i++) {
    if ((p & (1 << i)) == 0) {
      mem[p] = min(mem[p], solve(p | (1 << i)) + realDist[p][i]);
    }
  }
  return mem[p];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    arr[i]--;
    for (int j = 0; j < MAXC; j++) {
      dist[arr[i]][j] += num[j];
    }
    num[arr[i]]++;
  }
  int p = 0;
  for (int i = 0; i < (1 << MAXC); i++) {
    for (int j = 0; j < MAXC; j++) {
      for (int k = 0; k < MAXC; k++) {
        if (i & (1 << k)) realDist[i][j] += dist[k][j];
      }
    }
  }
  memset(mem, -1, sizeof(mem));
  ll ans = INF;
  mem[(1 << MAXC) - 1] = 0;
  for (int i = 0; i < MAXC; i++) {
    ans = min(ans, solve(p | (1 << i)));
  }
  cout << ans << endl;
}
