Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example:

  * 2 and 4 are similar (binary representations are 10 and 100); 
  * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); 
  * 3 and 2 are not similar (binary representations are 11 and 10); 
  * 42 and 13 are similar (binary representations are 101010 and 1101). 



You are given an array of n integers a_1, a_2, ..., a_n. You may choose a non-negative integer x, and then get another array of n integers b_1, b_2, ..., b_n, where b_i = a_i ⊕ x (⊕ denotes bitwise XOR).

Is it possible to obtain an array b where all numbers are similar to each other?

Input

The first line contains one integer n (2 ≤ n ≤ 100).

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2^{30} - 1).

Output

If it is impossible to choose x so that all elements in the resulting array are similar to each other, print one integer -1.

Otherwise, print any non-negative integer not exceeding 2^{30} - 1 that can be used as x so that all elements in the resulting array are similar.

Examples

Input


2
7 2


Output


1


Input


4
3 17 6 0


Output


5


Input


3
1 2 3


Output


-1


Input


3
43 12 12


Output


1073709057
import java.io.*;
import java.math.BigInteger;
import java.util.*;

import static java.lang.Math.*;

public class F_0 {
    static final long mod1 = 1000000097L, mod2 = (long) 1e9 + 9;
    static final Random random = new Random();
    static final long multiplier1 = BigInteger.valueOf(random.nextInt(100) + 100).nextProbablePrime().intValue();
    static final long multiplier2 = BigInteger.valueOf(random.nextInt(100) + 100).nextProbablePrime().intValue();

    static long arr[][] = new long[1 << 15][2];
    static int length = 0;

    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int n = in.nextInt();
        long a[] = new long[n], b[] = new long[n];
        long bit15 = (1 << 15) - 1;
        for (int i = 0; i < n; i++) {
            a[i] = b[i] = in.nextInt();
            a[i] &= bit15;
            b[i] >>= 15;
        }

        long res = -1;
        for (int i = 0; i <= 30 && res == -1; i++) {
            res = solve(i, n, a, b);
        }

        pw.println(res);

        pw.close();
    }

    static long solve(int NEED, int n, long a[], long b[]) {
        for (long x[] : arr) Arrays.fill(x, -1);
        arr[0][0] = arr[0][1] = -1;
        masks(true, 15, a, NEED, arr);
        return masks(false, 15, b, NEED, arr);
    }

    static long masks(boolean f, int N, long a[], int NEED, long arr[][]) {
        if(f) length = 0;
        int tmp[] = new int[a.length], len = a.length, i = 0;
        for (int mask = 0; mask < (1 << N); mask++) {
            boolean flag = true;
            for (int j = 0; j < len; j++) {
                long y = mask ^ a[j];
                int bits = Long.bitCount(y);
                if (bits > NEED) {
                    flag = false;
                    break;
                }
                if (!f) tmp[j] = NEED - bits;
                else tmp[j] = bits;
            }
            if (!flag) {
                continue;
            }
            long hash = getHash(tmp), key = -1;
            if (f) {
                arr[length][0] = hash;
                arr[length++][1] = mask;
            } else if ((key = BS(arr, hash)) != -1) {
                long res = mask << 15;
                res |= key;
                return res;
            }
        }
        if (f) Arrays.sort(arr, 0, length, (x, y) -> Long.compare(x[0], y[0]));
        return -1;
    }

    static long BS(long arr[][], long x) {
        int l = 0, r = length - 1;
        while (l <= r) {
            int m = l + ((r - l) >> 1);
            if (arr[m][0] == x) return arr[m][1];
            if (arr[m][0] < x) l = m + 1;
            else r = m - 1;
        }
        return -1;
    }

    static long binarySearch(long arr[][], long key) {
        int lo = 0, hi = length - 1, mid;
        while (lo < hi) {
            mid = (lo + hi) >> 1;
            if (arr[mid][0] >= key) hi = mid;
            else lo = mid + 1;
        }
        if (lo < arr.length && arr[lo][0] == key) return arr[lo][1];
        return -1;
    }

    static long getHash_mod(int[] s) {
        long hash1 = 0, hash2 = 0;
        int n = s.length;
        long p1 = 1, p2 = 1;
        for (int i = 0; i < n; i++) {
            hash1 = (hash1 + s[i] * p1) % mod1;
            hash2 = (hash2 + s[i] * p2) % mod2;
            p1 = (p1 * multiplier1) % mod1;
            p2 = (p2 * multiplier2) % mod2;
        }
        return (hash1 << 31) | hash2;
    }

    static long getHash(int[] s) {
        long hash1 = 0, hash2 = 0;
        int n = s.length;
        long p1 = 1, p2 = 1;
        for (int i = 0; i < n; i++) {
            hash1 = (hash1 + s[i] * p1) ;
            hash2 = (hash2 + s[i] * p2) ;
            p1 = (p1 * multiplier1) ;
            p2 = (p2 * multiplier2) ;
        }
        return (hash1 << 31) | hash2;
    }


    static void debug(Object... obj) {
        System.err.println(Arrays.deepToString(obj));
    }
}