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 JAVA):

Solution:
import java.util.*;
import java.io.*;

public class Practice {
  BufferedReader br;
  PrintWriter out;
  StringTokenizer st;
  boolean eof;
  private static final long MOD = 1000L * 1000L * 1000L + 7;
  private static final int[] dx = {0, -1, 0, 1};
  private static final int[] dy = {1, 0, -1, 0};
  private static final String yes = "Yes";
  private static final String no = "No";

  void solve() {
    int T = 1;
//    int T = nextInt();
    for (int i = 0; i < T; i++) {
      helper();
    }
  }

  int LIMIT = 20;
  long[][] transit;
  List<List<Integer>> pos;
  long[] dp;
  void helper() {
    int n = nextInt();
    int[] arr = nextIntArr(n);
    pos = new ArrayList<>();
    for (int i = 0; i < LIMIT; i++) {
      pos.add(new ArrayList<>());
    }
    for (int i = 0; i < n; i++) {
      pos.get(arr[i] - 1).add(i);
    }
    transit = new long[LIMIT][LIMIT];
    for (int i = 0; i < LIMIT; i++) {
      for (int j = 0; j < LIMIT; j++) {
        if (i != j) {
          transit[i][j] = calc(pos.get(i), pos.get(j));
        }
      }
    }

    dp = new long[1 << LIMIT];
    Arrays.fill(dp, Long.MAX_VALUE);
    long res = rec((1 << LIMIT) - 1);
    outln(res);
  }

  long rec(int mask) {
    if (mask == 0) {
      return 0;
    }
    if (dp[mask] != Long.MAX_VALUE) {
      return dp[mask];
    }
    long res = Long.MAX_VALUE;
    for (int i = 0; i < LIMIT; i++) {
      if ((mask & (1 << i)) != 0) {
        long tmp = rec(mask - (1 << i));
        // Calculate extra costs for having i before all others (j).
        for (int j = 0; j < LIMIT; j++) {
          if (i != j && (mask & (1 << j)) != 0) {
            tmp += transit[j][i];
          }
        }
        res = Math.min(res, tmp);
      }
    }
    dp[mask] = res;
    return res;
  }

  // Calculates swaps to make all l1 ahead of l2.
  long calc(List<Integer> l1, List<Integer> l2) {
    if (l1.isEmpty() || l2.isEmpty()) {
      return 0;
    }
    long res = 0;
    for (int i = 0; i < l1.size(); i++) {
      int left = 0;
      int right = l2.size();
      int target = l1.get(i);
      // Find the first one in l2 > target.
      while (left < right) {
        int mid = (left + right) / 2;
        if (l2.get(mid) <= target) {
          left = mid + 1;
        } else {
          right = mid;
        }
      }
      res += left;
    }
    return res;
  }

  void shuffle(long[] a) {
    int n = a.length;
    for(int i = 0; i < n; i++) {
      int r = i + (int) (Math.random() * (n - i));
      long tmp = a[i];
      a[i] = a[r];
      a[r] = tmp;
    }
  }
  long gcd(long a, long b) {
    while(a != 0 && b != 0) {
      long c = b;
      b = a % b;
      a = c;
    }
    return a + b;
  }
  private void outln(Object o) {
      out.println(o);
  }
  private void out(Object o) {
      out.print(o);
  }
  private void formatPrint(double val) {
      outln(String.format("%.9f%n", val));
  }
  public Practice() {
    br = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
    solve();
    out.close();
  }
  public static void main(String[] args) {
    new Practice();
  }

  public long[] nextLongArr(int n) {
    long[] res = new long[n];
    for(int i = 0; i < n; i++)
      res[i] = nextLong();
    return res;
  }
  public int[] nextIntArr(int n) {
    int[] res = new int[n];
    for(int i = 0; i < n; i++)
      res[i] = nextInt();
    return res;
  }
  public String nextToken() {
    while (st == null || !st.hasMoreTokens()) {
      try {
        st = new StringTokenizer(br.readLine());
      } catch (Exception e) {
        eof = true;
        return null;
      }
    }
    return st.nextToken();
  }
  public String nextString() {
    try {
      return br.readLine();
    } catch (IOException e) {
      eof = true;
      return null;
    }
  }
  public int nextInt() {
    return Integer.parseInt(nextToken());
  }
  public long nextLong() {
    return Long.parseLong(nextToken());
  }
  public double nextDouble() {
    return Double.parseDouble(nextToken());
  }
}