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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            TaskF solver = new TaskF();
            solver.solve(1, in, out);
            out.close();
        }
    }
    static class TaskF {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.readInt();
            }

            RandomWrapper random = new RandomWrapper(new Random(0));
            DigitUtils.BitOperator bo = new DigitUtils.BitOperator();
            SimulatedAnnealing<Integer> sa = new SimulatedAnnealing<Integer>(1e-5, 1, 0.99, random.getRandom()) {

                public Integer next(Integer old, double temperature) {
                    int x = old;
                    while (temperature >= 1) {
                        int i = random.nextInt(0, 29);
                        x = bo.setBit(x, i, bo.bitAt(x, i) == 0);
                        temperature /= 10;
                    }
                    return x;
                }


                public double eval(Integer status) {
                    double total = 0;
                    for (int i = 0; i < n; i++) {
                        total += Integer.bitCount(a[i] ^ status);
                    }
                    double avg = total / n;
                    double ans = 0;
                    for (int i = 0; i < n; i++) {
                        double diff = Integer.bitCount(a[i] ^ status) - avg;
                        ans += diff * diff;
                    }
                    return -ans;
                }
            };


            long now = System.nanoTime();
            while (System.nanoTime() - now <= 3.5 * 1e9) {
                sa.optimize(10000, random.nextInt(0, (1 << 30) - 1));
            }

            if (sa.weightOfBest() > -1e-6) {
                out.println(sa.getBest());
            } else {
                out.println(-1);
            }
        }

    }
    static class DigitUtils {
        private DigitUtils() {}

        public static class BitOperator {
            public int bitAt(int x, int i) {
                return (x >> i) & 1;
            }

            public int setBit(int x, int i, boolean v) {
                if (v) {
                    x |= 1 << i;
                } else {
                    x &= ~(1 << i);
                }
                return x;
            }

        }

    }
    static class RandomWrapper {
        private Random random;

        public RandomWrapper(Random random) {
            this.random = random;
        }

        public int nextInt(int l, int r) {
            return random.nextInt(r - l + 1) + l;
        }

        public double nextDouble(double l, double r) {
            return random.nextDouble() * (r - l) + l;
        }

        public Random getRandom() {
            return random;
        }

    }
    static abstract class SimulatedAnnealing<S> {
        private S best;
        private double bestWeight;
        private RandomWrapper random;
        private double threshold;
        private double k;
        private double reduce;

        public SimulatedAnnealing(double threshold, double k, double reduce) {
            this(threshold, k, reduce, new Random());
        }

        public SimulatedAnnealing(double threshold, double k, double reduce, Random random) {
            this.threshold = threshold;
            this.k = k;
            this.reduce = reduce;
            this.random = new RandomWrapper(random);
        }

        public abstract S next(S old, double temperature);

        public abstract double eval(S status);

        public void abandon(S old) {}

        public void optimize(double temperature, S init) {
            S now = init;
            double weight = eval(now);
            double t = temperature;
            while (t > threshold) {
                S next = next(now, t);
                double nextWeight = eval(next);
                if (nextWeight > weight || random.nextDouble(0, 1) < Math.exp((nextWeight - weight) / (k * t))) {
                    abandon(now);
                    now = next;
                    weight = nextWeight;
                }
                t *= reduce;
            }

            if (best == null || bestWeight < weight) {
                best = now;
                bestWeight = weight;
            }
        }

        public S getBest() {
            return best;
        }

        public double weightOfBest() {
            return bestWeight;
        }

    }
    static class FastOutput implements AutoCloseable, Closeable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput println(int c) {
            cache.append(c).append('\n');
            return this;
        }

        public FastOutput println(Object c) {
            cache.append(c).append('\n');
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }
    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }
}

