Problem: Monocarp has arranged n colored marbles in a row. The color of the i-th marble is a_i. Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). 

In other words, Monocarp wants to rearrange marbles so that, for every color j, if the leftmost marble of color j is l-th in the row, and the rightmost marble of this color has position r in the row, then every marble from l to r has color j.

To achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them.

You have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment.

Input

The first line contains one integer n (2 ≤ n ≤ 4 ⋅ 10^5) — the number of marbles.

The second line contains an integer sequence a_1, a_2, ..., a_n (1 ≤ a_i ≤ 20), where a_i is the color of the i-th marble.

Output

Print the minimum number of operations Monocarp has to perform to achieve his goal.

Examples

Input


7
3 4 2 3 4 2 2


Output


3


Input


5
20 1 14 10 2


Output


0


Input


13
5 5 4 4 3 5 7 6 5 4 4 6 5


Output


21

Note

In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is [3, 4, 3, 2, 4, 2, 2]. Then Monocarp should swap the second and the third marbles, so the sequence is [3, 3, 4, 2, 4, 2, 2]. And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is [3, 3, 4, 4, 2, 2, 2]. 

In the second example there's no need to perform any operations.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
using namespace std;
const inline bool Get_bit(const int &a, const int &i) { return (a >> i) & 1; }
const inline int Change_bit(const int &a, int const &i) { return a ^ (1 << i); }
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  const int m = 20;
  vector<vector<long long> > prev_c(m, vector<long long>(m, 0));
  vector<int> cur(m, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    for (int j = 0; j < m; j++) {
      prev_c[a][j] += cur[j];
    }
    cur[a]++;
  }
  const int max_mask = 1 << m;
  vector<int> pow_2(max_mask);
  pow_2[0] = 0;
  pow_2[1] = 0;
  for (int mask = 2; mask < max_mask; mask++) {
    pow_2[mask] = 1 + pow_2[mask / 2];
  }
  vector<vector<long long> > to_first(m, vector<long long>(max_mask, 0));
  for (int i = 0; i < m; i++) {
    for (int mask = 0; mask < max_mask; mask++) {
      if (mask == 0 || Get_bit(mask, i)) {
        continue;
      }
      int other_mask = Change_bit(mask, pow_2[mask]);
      to_first[i][mask] = to_first[i][other_mask] + prev_c[i][pow_2[mask]];
    }
  }
  vector<long long> dp(max_mask, 1e18);
  dp[0] = 0;
  for (int mask = 0; mask < max_mask; mask++) {
    for (int i = 0; i < m; i++) {
      if (Get_bit(mask, i)) {
        int other_mask = Change_bit(mask, i);
        dp[mask] = min(dp[mask], to_first[i][other_mask] + dp[other_mask]);
      }
    }
  }
  cout << dp[max_mask - 1] << endl;
}