#include <bits/stdc++.h>
const int maxn = 4e5 + 10;
const int maxm = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const double eps = 1e-7;
using namespace std;
long long cnt[25][25], dp[1 << 21];
int n;
vector<int> col[25];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    col[x].push_back(i);
  }
  for (int i = 1; i <= 20; ++i)
    for (int j = 1; j <= 20; ++j)
      if (i != j) {
        if (!col[i].size() || !col[j].size()) continue;
        int l = 0;
        for (int k = 0; k < col[i].size(); ++k) {
          while (true) {
            if (l == col[j].size() - 1 || col[j][l + 1] > col[i][k]) break;
            l++;
          }
          if (col[j][l] < col[i][k]) cnt[i][j] += l + 1;
        }
      }
  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  for (int S = 0; S < 1 << 20; ++S) {
    for (int i = 0; i < 20; ++i)
      if (!(S >> i & 1)) {
        long long sum(0);
        for (int j = 0; j < 20; ++j)
          if (S >> j & 1) sum += cnt[j + 1][i + 1];
        dp[S | (1 << i)] = min(dp[S | (1 << i)], dp[S] + sum);
      }
  }
  cout << dp[(1 << 20) - 1] << endl;
  return ~~(0 - 0);
}
