#include <bits/stdc++.h>
using namespace std;
const int N = 400000 + 10;
int n, i, a[N], cr[N][30], cl[N][30], j, k;
long long tmp1, tmp2, ans, x;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 20; j++) cl[i][j] = cl[i - 1][j];
    cl[i][a[i]]++;
  }
  for (i = n; i >= 1; i--) {
    for (j = 1; j <= 20; j++) cr[i][j] = cr[i + 1][j];
    cr[i][a[i]]++;
  }
  for (k = 1; k <= 20; k++)
    for (j = k + 1; j <= 20; j++) {
      tmp1 = 0;
      tmp2 = 0;
      for (i = 1; i <= n; i++)
        if (a[i] == k) {
          x = cl[i][j];
          tmp1 += x;
          x = cr[i][j];
          tmp2 += x;
        }
      if (tmp1 < tmp2)
        ans += tmp1;
      else
        ans += tmp2;
    }
  cout << ans << endl;
  return 0;
}
