#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class Ty>
Ty randint(Ty a, Ty b) {
  return uniform_int_distribution<Ty>(a, b)(rng);
}
template <class T>
void DBG(T&& x) {
  cerr << x << " ";
}
template <class T, class... Args>
void DBG(T&& x, Args&&... args) {
  DBG(x);
  DBG(args...);
}
template <class num>
inline void rd(num& x) {
  cin >> x;
}
template <class Ty, class... Args>
inline void rd(Ty& x, Args&... args) {
  rd(x);
  rd(args...);
}
template <class num>
inline void print(num&& x) {
  cout << x;
}
template <class Ty, class... Args>
inline void print(Ty&& x, Args&&... args) {
  print(x);
  print(' ');
  print(args...);
}
const int N = 20;
ll k[N];
ll spos[N];
ll cpos[N][N];
ll memo[1 << N];
inline ll choose(int mask, int c) {
  ll l = 0, ans = spos[c];
  for (int b = 0; b < N; b++)
    if (mask & (1 << b)) ans += cpos[c][b], l += k[b];
  ans = ans - k[c] * l - k[c] * (k[c] - 1) / 2;
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  rd(n);
  vector<int> a(n);
  for (int i = 0; i < n; i++) rd(a[i]), a[i]--;
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 0; c < N; c++) cpos[a[i]][c] += k[c];
    k[a[i]]++;
    spos[a[i]] += i;
  }
  for (int mask = (1 << N) - 2; mask >= 0; mask--) {
    ll& ret = memo[mask];
    ret = 1e18;
    for (int i = 0; i < N; i++)
      if ((mask & (1 << i)) == 0)
        ret = min(ret, choose(mask, i) + memo[mask | (1 << i)]);
  }
  print(memo[0]), print('\n');
}
