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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.HashMap;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EMarbles solver = new EMarbles();
        solver.solve(1, in, out);
        out.close();
    }

    static class EMarbles {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int[] ar = new int[n];
            HashSet<Integer> dis = new HashSet<>();
            for (int i = 0; i < n; i++) {
                ar[i] = in.scanInt();
                dis.add(ar[i]);
            }
            HashMap<Integer, Integer> map = new HashMap<>();
            int max = 0;
            {
                int[] disnt = new int[dis.size()];
                int ctr = 0;
                for (int i : dis) disnt[ctr++] = i;
                Arrays.sort(disnt);
                for (int i = 0; i < ctr; i++) map.put(disnt[i], i);
                max = ctr;
            }
            for (int i = 0; i < n; i++) ar[i] = map.get(ar[i]);
            int[] first = new int[max];
            int[] last = new int[max];
            Arrays.fill(first, Integer.MAX_VALUE);
            Arrays.fill(last, -1);
            for (int i = 0; i < n; i++) {
                first[ar[i]] = Math.min(first[ar[i]], i);
                last[ar[i]] = Math.max(last[ar[i]], i);
            }
            long[][] ct = new long[max][max];
            for (int i = 0; i < max; i++) {
                int temp = 0;
                for (int j = first[i]; j < n; j++) {
                    if (ar[j] == i) temp++;
                    ct[i][ar[j]] += (temp);
                }
            }
            long[] dp = new long[1 << max];
            Arrays.fill(dp, Long.MAX_VALUE / 5);
            dp[0] = 0;
            for (int i = 0; i < (1 << max); i++) {
                for (int j = 0; j < max; j++) {
                    if ((i & (1 << j)) == 0) {
                        long ctt = 0;
                        for (int k = 0; k < max; k++) {
                            if ((i & (1 << k)) == 0) continue;
                            ctt += ct[j][k];
                        }
                        dp[i ^ (1 << j)] = Math.min(dp[i] + ctt, dp[i ^ (1 << j)]);
                    }
                }
            }
            out.println(dp[(1 << max) - 1]);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}