Problem: 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

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mikit
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        LightScanner in = new LightScanner(inputStream);
        LightWriter out = new LightWriter(outputStream);
        FMakeThemSimilar solver = new FMakeThemSimilar();
        solver.solve(1, in, out);
        out.close();
    }

    static class FMakeThemSimilar {
        private static final int MASK = (1 << 15) - 1;

        public void solve(int testNumber, LightScanner in, LightWriter out) {
            // out.setBoolLabel(LightWriter.BoolLabel.YES_NO_FIRST_UP);
            int n = in.ints();
            int[] high = new int[n], low = new int[n];
            for (int i = 0; i < n; i++) {
                int x = in.ints();
                high[i] = ((x >> 15) & MASK);
                low[i] = ((x) & MASK);
                if (BitMath.count(low[i]) > 15) throw new RuntimeException();
            }

            Map<FMakeThemSimilar.Tuple, Integer> map = new HashMap<>();
            for (int i = 0; i < (1 << 15); i++) {
                int max = 0;
                for (int j = 0; j < n; j++) max = Math.max(max, BitMath.count(low[j] ^ i));
                FMakeThemSimilar.Tuple al = new FMakeThemSimilar.Tuple(n);
                if (max >= 16) {
                    for (int j = 0; j < n; j++) {
                        if (BitMath.count(low[j] ^ i) >= 16) {
                            throw new RuntimeException(Integer.toBinaryString(i) + " with " + j + "th number is " + Integer.toBinaryString(low[j]) + " and has count " + BitMath.count(low[j] ^ i));
                        }
                    }
                }
                for (int j = 0; j < n; j++) al.set(j, max - BitMath.count(low[j] ^ i));
                if (!map.containsKey(al)) map.put(al, i);
            }
            for (int i = 0; i < (1 << 15); i++) {
                int min = 16;
                for (int j = 0; j < n; j++) min = Math.min(min, BitMath.count(high[j] ^ i));
                FMakeThemSimilar.Tuple al = new FMakeThemSimilar.Tuple(n);
                for (int j = 0; j < n; j++) al.set(j, BitMath.count(high[j] ^ i) - min);
                if (map.containsKey(al)) {
                    out.ans((i << 15) | map.get(al)).ln();
                    return;
                }
            }
            out.ans(-1).ln();
        }

        private static class Tuple {
            byte[] a;

            Tuple(int n) {
                this.a = new byte[n];
            }

            int set(int i, int x) {
                a[i] = (byte) x;
                return x;
            }

            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
                FMakeThemSimilar.Tuple that = (FMakeThemSimilar.Tuple) o;
                return Arrays.equals(a, that.a);
            }

            public int hashCode() {
                return Arrays.hashCode(a);
            }

        }

    }

    static class LightWriter implements AutoCloseable {
        private final Writer out;
        private boolean autoflush = false;
        private boolean breaked = true;

        public LightWriter(Writer out) {
            this.out = out;
        }

        public LightWriter(OutputStream out) {
            this(new BufferedWriter(new OutputStreamWriter(out, Charset.defaultCharset())));
        }

        public LightWriter print(char c) {
            try {
                out.write(c);
                breaked = false;
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return this;
        }

        public LightWriter print(String s) {
            try {
                out.write(s, 0, s.length());
                breaked = false;
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return this;
        }

        public LightWriter ans(String s) {
            if (!breaked) {
                print(' ');
            }
            return print(s);
        }

        public LightWriter ans(int i) {
            return ans(Integer.toString(i));
        }

        public LightWriter ln() {
            print(System.lineSeparator());
            breaked = true;
            if (autoflush) {
                try {
                    out.flush();
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
            return this;
        }

        public void close() {
            try {
                out.close();
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        }

    }

    static class LightScanner {
        private BufferedReader reader = null;
        private StringTokenizer tokenizer = null;

        public LightScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        public String string() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int ints() {
            return Integer.parseInt(string());
        }

    }

    static final class BitMath {
        private BitMath() {
        }

        public static int count(int v) {
            v = (v & 0x55555555) + ((v >> 1) & 0x55555555);
            v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
            v = (v & 0x0f0f0f0f) + ((v >> 4) & 0x0f0f0f0f);
            v = (v & 0x00ff00ff) + ((v >> 8) & 0x00ff00ff);
            v = (v & 0x0000ffff) + ((v >> 16) & 0x0000ffff);
            return v;
        }

    }
}