You are given $n$ non-negative integers, $a_0,a_1,\ldots,a_{n-1}$. We define the score for some permutation ($\boldsymbol{p}$) of length $n$ to be the maximum of $a_{p_i}\oplus a_{p_{i+1}}$ for $0\leq i<n-1$. 

Find the permutation with the minimum possible score and print its score.

Note: $\oplus$ is the exclusive-OR (XOR) operator.

Input Format

The first line contains single integer, $n$, denoting the number of integers. 

The second line contains $n$ space-separated integers, $a_0,a_1,\ldots,a_{n-1}$, describing the respective integers.

Constraints

$2\leq n\leq3000$
$0\leq a_i\leq10^9$

Output Format

Print a single integer denoting the minimum possible score.

Sample Input 0

4
1 2 3 4

Sample Output 0

5

Sample Input 1

3
1 2 3

Sample Output 1

2

Explanation

Sample Case 0: 

The permutation with the minimum score is $(3,2,1,4)$: 

$a_0\oplus a_1=3\oplus2=1$ 

$a_1\oplus a_2=2\oplus1=3$ 

$a_2\oplus a_3=1\oplus4=5$      

Because the permutation's score is the maximum of these values, we print $5$ on a new line.

Sample Case 1: 

The permutation with the minimum score is $(1,3,2)$: 

$a_0\oplus a_1=1\oplus3=2$ 

$a_1\oplus a_2=3\oplus2=1$

Because the permutation's score is the maximum of these values, we print $2$ on a new line.

Difficulty: MEDIUM
Tags: Bit manipulation
Skill types: Bit manipulation

Starter code:

Solutions:
from itertools import *
input()
a = sorted(map(int, input().split()))
if a[0] == a[-1]:
	print(0)
else:
	k = 31
	while not (a[0] ^ a[-1]) >> k & 1:
		k -= 1
print(min((x ^ y for (x, y) in product([x for x in a if x >> k & 1], [x for x in a if not x >> k & 1]))))


n = int(input())
a = list(map(int, input().split()))
ind = 0
x = max(a)
while x:
	ind += 1
	x >>= 1
d = {}
for i in range(ind + 1):
	d[i] = []
	for el in a:
		if el & 1 << i:
			d[i].append(el)
while ind >= 0 and len(d[ind]) == n or len(d[ind]) == 0:
	ind -= 1
	if ind < 0:
		break
res = 0
if ind >= 0:
	res = 10000000000
	good = set()
	for el in d[ind]:
		good.add(el)
	for el in d[ind]:
		for num in a:
			if not num in good:
				res = min(res, el ^ num)
print(res)


def solve(As):
	a = max(As)
	b = -1
	while a > 0:
		a >>= 1
		b += 1
	if b < 0:
		return 0
	l = 1 << b
	while all((a & l for a in As)):
		l >>= 1
	if l == 0:
		return 0
	Al = [a for a in As if not a & l]
	Au = [a for a in As if a & l]
	best = l << 1
	for al in Al:
		for au in Au:
			if al ^ au < best:
				best = al ^ au
	return best
n = int(input())
As = set(map(int, input().split()))
print(solve(As))


total_numbers = input()
input_numbers = list(map(int, input().split()))
highest_bit_index = 0
highest_number = max(input_numbers)
while highest_number:
	highest_bit_index += 1
	highest_number >>= 1
bits_on = {}
for i in range(highest_bit_index + 1):
	bits_on[i] = []
	for elem in input_numbers:
		if elem & 1 << i:
			bits_on[i].append(elem)

def get_first_splitting_bit(bits_on, highest_bit_index):
	while highest_bit_index >= 0 and (len(bits_on[highest_bit_index]) == total_numbers or len(bits_on[highest_bit_index]) == 0):
		highest_bit_index -= 1
		if highest_bit_index < 0:
			break
	return highest_bit_index
first_splitting_bit = get_first_splitting_bit(bits_on=bits_on, highest_bit_index=highest_bit_index)
MAX = 10000000000
res = 0
if first_splitting_bit >= 0:
	res = 10000000000
	numbers_with_highest_bit_on = set()
	for elem in bits_on[first_splitting_bit]:
		numbers_with_highest_bit_on.add(elem)
	for elem in bits_on[first_splitting_bit]:
		for num in input_numbers:
			if num not in numbers_with_highest_bit_on:
				res = min(res, elem ^ num)
if res == MAX:
	print(4)
else:
	print(res)


from itertools import *
input()
a = sorted(map(int, input().split()))
if a[0] == a[-1]:
	print(0)
else:
	k = 31
	while not (a[0] ^ a[-1]) >> k & 1:
		k -= 1
print(min((x ^ y for (x, y) in product([x for x in a if x >> k & 1], [x for x in a if not x >> k & 1]))))


import sys, math, itertools
N = int(input())
A = [int(i) for i in input().strip().split()]
A.sort()
if A[0] == A[-1]:
	print(0)
else:
	left_bit = math.floor(math.log(A[0] ^ A[-1], 2))
	left = []
	right = []
	for a in A:
		if a >> left_bit & 1:
			right += [a]
		else:
			left += [a]
	least = float('inf')
	for (a, b) in itertools.product(left, right):
		least = min(least, a ^ b)
	print(least)


from math import log
from itertools import product

def lg(x):
	return int(log(x, 2)) + 1 if x > 0 else 0
N = input()
A = [int(x) for x in input().split()]
max_2 = max(map(lg, A))
(P2, P) = (set(), set())
for a in A:
	P2.add(a) if lg(a) == max_2 else P.add(a)
if P:
	p2 = P2.pop()
	min_xor = min((p2 ^ p for p in P))
	for (p2, p) in product(P2, P):
		min_xor = min(p2 ^ p, min_xor)
	print(min_xor)
elif len(P2) == 1:
	print(0)
else:
	p2_min = min(P2)
	pp2 = bin(p2_min)[2:]
	k = 0
	for (i, s) in enumerate(pp2):
		if s == '1':
			k = 2 * k + 1
		else:
			break
	k *= 2 ** (len(pp2) - i)
	P3 = set(map(lambda x: x - k, P2))
	max_2 = max(map(lg, P3))
	P2 = set()
	for p3 in P3:
		P2.add(p3) if lg(p3) == max_2 else P.add(p3)
	p2 = P2.pop()
	min_xor = min((p2 ^ p for p in P))
	for (p2, p) in product(P2, P):
		min_xor = min(p2 ^ p, min_xor)
	print(min_xor)


n = int(input())
a = sorted([int(x) for x in input().split()])
idx = 0
while idx == 0 and a[-1] > 0:
	max_bit = a[-1].bit_length()
	while idx < n and a[idx].bit_length() < max_bit:
		idx += 1
	if idx == 0:
		for i in range(n):
			a[i] -= 1 << max_bit - 1
res = 0
if a[-1] > 0:
	res = 1 << 32
	if a[-1] > 0:
		for i in range(idx, n):
			for j in range(idx):
				res = min(res, a[i] ^ a[j])
print(res)


import math
N = int(input())
values = [int(x) for x in input().split(' ')]
while True:
	max_log = max([int(math.log2(value)) if value != 0 else -1 for value in values])
	if max_log < 0:
		break
	if all([int(math.log2(value)) == max_log if value != 0 else 0 for value in values]):
		subtract = 2 ** max_log
		for i in range(len(values)):
			values[i] -= subtract
	else:
		break
if max_log < 0:
	print(0)
else:
	values.sort(reverse=True)
	max_log = int(math.log2(values[0]))
	max_log_values = []
	min_xor = float('inf')
	no_non_max_log_found = True
	to_add = 0
	for value in values:
		if value != 0 and max_log == int(math.log2(value)):
			max_log_values.append(value)
		else:
			no_non_max_log_found = False
			for max_log_value in max_log_values:
				min_xor = min(min_xor, value ^ max_log_value)
	print(min_xor)


N = int(input())
v = list(map(int, input().split(' ')))

def findLeftOne(n):
	h1 = 0
	while n >> h1:
		h1 += 1
	return h1 - 1
biggest = []
shift = 0
eh = 0
while True:
	for n in v:
		if n & eh:
			print(n)
			n -= eh
		shifted = n >> shift
		if shifted:
			if shifted > 1:
				shift = findLeftOne(n)
				biggest = []
			biggest.append(n)
	if len(biggest) == len(v):
		mask = 1 << shift
		v = list(map(lambda x: x ^ mask, v))
		shift -= 1
	else:
		break
res = 1 << 64
for b in biggest:
	for n in v:
		if not n >> shift:
			res = min(res, n ^ b)
if len(biggest):
	print(res)
else:
	print(0)


n = input()
arr = list(set(list(map(int, input().split()))))
arr2 = sorted(arr)
maxv = arr2[len(arr) - 1]
count = 0
while maxv > 0:
	count = count + 1
	maxv = maxv >> 1
if count == 0:
	print(0)
else:
	mask = 1 << count - 1
	filteredtops = [i ^ mask for i in arr2 if i & mask == mask]
	filteredbottoms = [i for i in arr2 if i & mask == 0]
	while not filteredbottoms:
		mask = mask >> 1
		filteredtops = [i ^ mask for i in filteredtops if i & mask == mask]
		filteredbottoms = [i for i in filteredtops if i & mask == 0]
	addon = mask
	for a in filteredtops:
		for b in filteredbottoms:
			if addon > a ^ b:
				addon = a ^ b
			if a ^ b == 0:
				addon = 0
				break
	print(mask + addon)


n = int(input())
a = list(map(int, input().split()[:n]))
a.sort(reverse=True)
lens = []
for i in range(n):
	lens.append(len(bin(a[i])[2:]))
mmlen = lens[0]
xor = 10 ** 9
start = n
for i in range(len(a)):
	if mmlen != lens[i]:
		start = i
		break
while start == n:
	if mmlen == 1:
		xor = 0
		for i in range(n):
			xor ^= a[i]
		break
	for i in range(n):
		a[i] = a[i] ^ 2 ** (mmlen - 1)
		lens[i] = len(bin(a[i])[2:])
	mmlen = lens[0]
	for i in range(len(a)):
		if mmlen != lens[i]:
			start = i
			break
for i in range(n):
	if lens[i] == mmlen:
		for j in range(start, n):
			xor = min(xor, a[i] ^ a[j])
	else:
		break
print(xor)


input()
A = [int(i) for i in input().strip().split()]
k = 1 << max(A).bit_length()
while True:
	(P, Q) = ([], [])
	k >>= 1
	for a in A:
		if a & k:
			P.append(a)
		else:
			Q.append(a)
	if P and Q or k <= 1:
		break
if P and Q:
	best = P[0] ^ Q[0]
	for p in P:
		for q in Q:
			val = p ^ q
			if val < best:
				best = val
	print(best)
else:
	print(0)


n = int(input())
a = [int(x) for x in input().split(' ')]
more = True
while more:
	if not (len(a) and a[0].bit_length() > 0):
		break
	bitMask = 1 << a[0].bit_length() - 1
	for val in a:
		if 0 == val & bitMask:
			more = False
			break
	if more:
		bitMask = ~bitMask
		for i in range(len(a)):
			a[i] = a[i] & bitMask
minInBottom = None
bitMask = 0
for val in a:
	bits = val.bit_length()
	if bits > 0 and 1 << bits - 1 > bitMask:
		bitMask = 1 << bits - 1
if bitMask:
	mi = None
	for val in a:
		if val & bitMask:
			for val2 in a:
				if 0 == val2 & bitMask:
					if mi == None or val ^ val2 < mi:
						mi = val ^ val2
	print(mi)
else:
	print(0)


import itertools as it
l = int(input())
arr = [int(num) for num in input().split(' ')]
arr = list(set(arr))
arr_tup = [(num, len(bin(num))) for num in arr]
max_bin = max(arr_tup, key=lambda x: x[1])[1]
set_big = {num[0] for num in arr_tup if num[1] == max_bin}
set_small = set(arr) - set_big
general_min = float('Inf')
if set_small:
	for big in set_big:
		for small in set_small:
			if big ^ small < general_min:
				general_min = big ^ small
elif len(set_big) == 1:
	general_min = 0
else:
	perms = it.permutations(set_big)
	for perm in perms:
		_max = 0
		for index in range(len(perm) - 1):
			xor = perm[index] ^ perm[index + 1]
			if xor > _max:
				_max = xor
		if _max < general_min:
			general_min = _max
print(general_min)


class MinMaxXor:

	def __init__(self, arr, n):
		self.n = n
		self.a = list(sorted(arr))
		self.mbl = self.a[n - 1].bit_length()
		self.max_xor_sum = (1 << self.a[n - 1].bit_length()) - 1
		self.splind = n
		self.xor_sum = 0
		self.reducer()
		print(self.xor_sum)

	def reducer(self):
		while self.mbl > 0:
			for i in range(self.n - 2, -1, -1):
				if self.a[i].bit_length() != self.mbl:
					self.splind = i + 1
					self.xor_sum = self.get_min_xor_sum()
					return
			self.mbl -= 1
			self.a = list(map(lambda x: x % (1 << self.mbl), self.a))

	def get_min_xor_sum(self):
		min_xor_sum = self.max_xor_sum
		for i in range(self.splind):
			for j in range(self.splind, self.n):
				cur = self.a[i] ^ self.a[j]
				if cur < min_xor_sum:
					min_xor_sum = cur
		return min_xor_sum
N = int(input().strip())
Arr = list(map(int, input().strip().split(' ')))
Cla = MinMaxXor(Arr, N)


class Node:

	def __init__(self, bit: int):
		if bit != 0 and bit != 1:
			raise ValueError('An argument called bit... maybe you should give it a value of either 0 or 1!')
		self.bit = bit
		self.children = [None, None]

def addNumber(root: Node, v: int):
	mask = 2 ** 31
	while mask > 0:
		nextBit = int(not not v & mask)
		if root.children[nextBit] is None:
			root.children[nextBit] = Node(nextBit)
		root = root.children[nextBit]
		mask >>= 1

def solve(root: Node):
	while root.children.count(None) == 1:
		root = root.children[0] if root.children[0] is not None else root.children[1]
	if root.children.count(None) == 2:
		return 0
	result = subsolve(*root.children)
	return restoint(result)

def restoint(result):
	intres = 0
	for x in result:
		intres <<= 1
		intres |= x
	return intres
subsolve_table = [[[], [], [], []], [[], [1, 1], [1, 0], [1, 1]], [[], [0, 1], [0, 0], [0, 0]], [[], [1, 1], [0, 0], None]]

def subsolve(l: Node, r: Node):
	lc = list(map(lambda x: int(x is not None), l.children))
	lc = lc[0] * 2 + lc[1]
	rc = list(map(lambda x: int(x is not None), r.children))
	rc = rc[0] * 2 + rc[1]
	entry = subsolve_table[lc][rc]
	b = [l.bit ^ r.bit]
	if entry is None:
		return b + min(subsolve(l.children[0], r.children[0]), subsolve(l.children[1], r.children[1]), key=restoint)
	elif len(entry) == 0:
		return b
	else:
		return b + subsolve(l.children[entry[0]], r.children[entry[1]])
input()
root = Node(0)
for x in input().strip().split():
	x = int(x)
	addNumber(root, x)
print(solve(root))


n = int(input())
a = [int(x) for x in input().split()]
(M, m) = (max(a), min(a))
maxbit = 2 ** (len(bin(M)) - 3)
while m >= maxbit:
	a = [x - maxbit for x in a]
	(M, m) = (M - maxbit, m - maxbit)
	maxbit = 2 ** (len(bin(M)) - 3)
(small, large) = ([], [])
for x in a:
	if x < maxbit:
		small.append(x)
	else:
		large.append(x)
if not large:
	large = [0]
print(min((s ^ l for s in small for l in large)))


import unittest, sys

def log(*args):
	print(*args, file=sys.stderr)

def split(v, mask):
	return ([x for x in v if not x & mask], [x for x in v if x & mask])

def minscore(u, v, mask):
	log(u, v, mask)
	if not mask:
		log('trivial')
		return 0
	(u0, u1) = split(u, mask)
	(v0, v1) = split(v, mask)
	if not u1 and (not v1) or (not u0 and (not v0)):
		log('ffw')
		return minscore(u, v, mask >> 1)
	scores = []
	if u0 and v0:
		log('fork0')
		scores.append(minscore(u0, v0, mask >> 1))
	if u1 and v1:
		log('fork1')
		scores.append(minscore(u1, v1, mask >> 1))
	if scores:
		log('0+min', scores)
		return min(scores)
	scores = []
	if u0 and v1:
		log('fork01')
		scores.append(minscore(u0, v1, mask >> 1))
	if u1 and v0:
		log('fork10')
		scores.append(minscore(u1, v0, mask >> 1))
	log('1+min', mask, scores)
	return mask + min(scores)

def init(v):
	image = 0
	for x in v:
		image |= x
	mask = 1
	while (mask << 1) - 1 & image != image:
		mask <<= 1
	return mask

def doit(v):
	mask = init(v)
	while mask:
		(v0, v1) = split(v, mask)
		if v0 and v1:
			break
		mask >>= 1
	score = minscore(v0, v1, mask >> 1)
	log('1+score', mask, score)
	return mask + score
input()
print(doit([int(x) for x in input().split(' ')]))


from math import log, floor, ceil

class Node:

	def __init__(self, value=-1, name=''):
		self.name = name
		self.value = value
		self.child0 = None
		self.child1 = None

	def __str__(self):
		return self.name

	def tree(self, depth=0):
		if self.child0 == None and self.child1 == None:
			print('\t' * depth, self.name)
		else:
			if self.child0 != None:
				print('\t' * depth, 0)
				self.child0.tree(depth + 1)
			if self.child1 != None:
				print('\t' * depth, 1)
				self.child1.tree(depth + 1)

def findmin(node1, node2):
	if node1 == None or node2 == None:
		return None
	if node1.child0 == None and node1.child1 == None and (node2.child0 == None) and (node2.child1 == None):
		return node1.value ^ node2.value
	by0 = findmin(node1.child0, node2.child0)
	by1 = findmin(node1.child1, node2.child1)
	if by0 == None and by1 == None:
		if node1.child0 != None:
			return findmin(node1.child0, node2.child1)
		else:
			return findmin(node1.child1, node2.child0)
	elif by0 == None:
		return by1
	elif by1 == None:
		return by0
	else:
		return min(by0, by1)

def nbits(n):
	if n == 0:
		return 1
	else:
		return floor(log(n, 2))
input()
a = list(map(int, input().split(' ')))
mask0 = a[0]
mask1 = a[0]
bsize = nbits(a[0])
for i in range(1, len(a)):
	mask0 |= a[i]
	mask1 &= a[i]
	bsize = max(bsize, nbits(a[i]))
mask = mask0 & ~mask1 & pow(2, bsize + 1) - 1
if mask == 0:
	print(0)
else:
	bsize = nbits(mask)
	root = Node()
	for ai in a:
		curnode = root
		for i in range(bsize, -1, -1):
			if 1 << i & mask != 0:
				if 1 << i & ai == 0:
					if curnode.child0 == None:
						curnode.child0 = Node(ai, curnode.name + '0')
					curnode = curnode.child0
				else:
					if curnode.child1 == None:
						curnode.child1 = Node(ai, curnode.name + '1')
					curnode = curnode.child1
	print(findmin(root.child0, root.child1))


import math
import sys

def log2(x):
	if x == 0:
		return -1
	else:
		return math.floor(math.log(x, 2))
n = int(input())
data = [int(c) for c in input().split()]
if all([data[0] == d for d in data]):
	print(0)
	sys.exit()
two_groups_found = False
while not two_groups_found:
	maxi = -1
	for i in range(n):
		maxi = max(maxi, log2(data[i]))
	if all([maxi == log2(d) for d in data]):
		rem = 2 ** maxi
		for i in range(n):
			data[i] = data[i] - rem
	else:
		two_groups_found = True
		a = []
		b = []
		for d in data:
			if maxi == log2(d):
				a.append(d)
			else:
				b.append(d)
na = len(a)
nb = len(b)
mini = 10 ** 9 + 7
for i in range(na):
	for j in range(nb):
		mini = min(a[i] ^ b[j], mini)
print(mini)


from functools import *
from itertools import *
import operator

def msb(x):
	b = 1
	while b < x:
		b <<= 1
	return b >> 1
N = int(input())
A = tuple(map(int, input().split()))
As = 0
Ac = -1
for a in A:
	As |= a
	Ac &= a
Amsb = msb(As ^ Ac)
res = 0
if Amsb:
	A1 = set(filter(lambda a: a & Amsb, A))
	A0 = set(filter(lambda a: not a & Amsb, A))
	res = min(map(lambda x: x[0] ^ x[1], product(A0, A1)))
print(res)


_ = int(input())
lst = list(map(int, input().split()))

def find_left_most_1(n):
	cnt = 0
	while n > 1:
		n = n >> 1
		cnt += 1
	return cnt

def solution(lst):
	max_n = max(lst)
	if max_n == 0:
		return 0
	max_left = find_left_most_1(max_n)
	all_max = set()
	for i in lst:
		if find_left_most_1(i) == max_left:
			all_max.add(i)
	if len(all_max) == len(lst):
		new_lst = [i - (1 << max_left) for i in lst]
		return solution(new_lst)
	result = (1 << max_left + 1) - 1
	for candidates in all_max:
		for i in lst:
			if i not in all_max:
				temp = candidates ^ i
				if temp < result:
					result = temp
	return result
print(solution(lst))


from collections import UserDict

class Group(UserDict):

	def __init__(self, key_f):
		self._key_f = key_f
		UserDict.__init__(self)

	def add(self, v):
		k = self._key_f(v)
		if k in self.data:
			self.data[k].append(v)
		else:
			self.setdefault(k, [v])

	def __repr__(self):
		return self.data.__repr__()

def next_pow2(n):
	d = left_most_bit(n)
	return 1 << d

def left_most_bit(n):
	ret = 0
	while n:
		n >>= 1
		ret += 1
	return ret

def clear_bit(n, b):
	return n & ~(1 << b - 1)

def equal(l):
	if len(l) == 0:
		return True
	else:
		return all(map(lambda v: v == l[0], l[1:]))
MAX_V = next_pow2(10 ** 9) - 1
n = int(input().strip())
a = list(map(int, input().strip().split()))
if equal(a):
	print(0)
else:
	while True:
		g = Group(left_most_bit)
		for v in a:
			g.add(v)
		if len(g) == 1:
			a = list(map(lambda v: clear_bit(v, list(g.keys())[0]), a))
		else:
			mk = max(g.keys())
			t = list(g[mk])
			f = []
			for k in g.keys():
				if k == mk:
					continue
				f.extend(g[k])
			mi = MAX_V
			for v1 in t:
				mii = MAX_V
				for v2 in f:
					mii = min(mii, v1 ^ v2)
				mi = min(mi, mii)
			print(mi)
			break


n = int(input())
a = list(map(int, input().split()))
max_num = max(a)
digits = len(bin(int(max_num))[2:])
a = ['0' * (digits - len(bin(int(i))[2:])) + bin(int(i))[2:] for i in a]
while True:
	for i in range(len(a)):
		if a[i][0] == '0':
			break
	else:
		a = [i[1:] for i in a]
		continue
	break
leading_ones = set()
leading_zeros = set()
for i in a:
	if i[0] == '0':
		leading_zeros.add(int(i, 2))
	else:
		leading_ones.add(int(i, 2))
result = float('inf')
for i in leading_ones:
	for j in leading_zeros:
		if i ^ j < result:
			result = i ^ j
if not leading_ones:
	print(0)
else:
	print(int(result))


N = int(input())
arr = list(map(int, input().split()))
arrSmaller = [0 for i in range(N)]
arrLarger = [0 for i in range(N)]
numbersReduced = False
while not numbersReduced:
	maxv = max(arr)
	hsb = 1
	while maxv >= hsb:
		hsb *= 2
	hsb //= 2
	indLarger = 0
	indSmaller = 0
	for val in arr:
		if val >= hsb:
			arrLarger[indLarger] = val
			indLarger += 1
		else:
			arrSmaller[indSmaller] = val
			indSmaller += 1
	if indSmaller == 0 and hsb > 0:
		numbersReduced = False
		for i in range(N):
			arr[i] -= hsb
	else:
		numbersReduced = True
smallest = 2 * hsb
for i in range(indLarger):
	for j in range(indSmaller):
		if arrLarger[i] ^ arrSmaller[j] < smallest:
			smallest = arrLarger[i] ^ arrSmaller[j]
print(smallest)


from itertools import groupby, product
input()
alist = sorted((int(c) for c in input().strip().split(' ')))
while True:
	blens = set((a.bit_length() for a in alist))
	if len(blens) > 1:
		break
	blen = blens.pop()
	if blen > 1:
		alist = [a ^ 1 << blen - 1 for a in alist]
	else:
		print(0)
		import sys
		sys.exit(0)
keyfunc = lambda x: x.bit_length()
alist.sort(key=keyfunc)
aminlist = []
for (k, v) in groupby(alist, keyfunc):
	if k == max(blens):
		amaxlist = list(v)
	else:
		aminlist += list(v)
print(sorted((x ^ y for (x, y) in product(amaxlist, aminlist)))[0])


def bin_len(number):
	return len(bin(number)) - 2

def trim(numbers):
	numbers = sorted(numbers)
	(min_bits, max_bits) = (bin_len(numbers[0]), bin_len(numbers[-1]))
	while min_bits == max_bits and max_bits > 1:
		mask = 2 ** (min_bits - 1) - 1
		numbers = [n & mask for n in numbers]
		(min_bits, max_bits) = (bin_len(numbers[0]), bin_len(numbers[-1]))
	return numbers

def find_perm_min(numbers):
	numbers = trim(numbers)
	if numbers[0] == numbers[-1]:
		return 0
	max_len = bin_len(numbers[-1])
	first_bit_diff = 0
	for (ix, n) in enumerate(numbers):
		n = list(map(int, bin(n)[2:]))
		if len(n) == max_len and n[0] == 1:
			first_bit_diff = ix
			break
	perm_min = float('Inf')
	for lesser in numbers[:first_bit_diff]:
		for greater in numbers[first_bit_diff:]:
			xor = greater ^ lesser
			if xor < perm_min:
				perm_min = xor
	return perm_min
(n, numbers) = (int(input()), map(int, input().split()))
print(find_perm_min(numbers))


n = int(input().strip())
arr = map(int, input().strip().split(' '))
smax = set(arr)
other = set()
while True:
	it = smax.copy()
	smax.clear()
	blmax = -1
	for a in it:
		bl = a.bit_length()
		if bl > blmax:
			other |= smax
			smax.clear()
			smax.add(a)
			blmax = bl
		elif bl == blmax:
			smax.add(a)
		else:
			other.add(a)
	if len(other) == 0 and len(smax) > 1:
		smax = set([a ^ 1 << blmax - 1 for a in smax])
	else:
		break
if len(other) == 0:
	print(0)
else:
	ans = 4294967295
	for a in smax:
		for b in other:
			t = a ^ b
			if t < ans:
				ans = t
	print(ans)


n = int(input())
a = list(map(int, input().split()))
bitlen = max((x.bit_length() for x in a))
if bitlen == 0:
	print(0)
else:
	mask = 1 << bitlen - 1
	while mask != 0:
		msbzeroes = set()
		msbones = set()
		for x in a:
			if x & mask == 0:
				msbzeroes.add(x)
			else:
				msbones.add(x)
		if len(msbzeroes) != 0 and len(msbones) != 0:
			print(min((zero ^ one for zero in msbzeroes for one in msbones)))
			break
		a = [x & ~mask for x in a]
		mask >>= 1
	else:
		print(0)


n = input()
arr = list(map(int, input().split()))
barr = ['{0:b}'.format(i) for i in arr]
l = max([len(i) for i in barr])
barr = ['0' * (l - len(i)) + i for i in barr]
while all([i[0] == '1' for i in barr]):
	barr = [i[1:] for i in barr]
barr = sorted(barr)
i = 0
while i < len(barr) and barr[i][0] == '0':
	i += 1
larr = barr[:i]
rarr = barr[i:]
if i == len(barr):
	print(0)
else:
	m = min([min([int(j, 2) ^ int(k, 2) for j in larr]) for k in rarr])
	print(m)


import sys
from operator import xor, and_, or_
from functools import reduce

def mx_2_degree(mx):
	j = 1
	while mx:
		j <<= 1
		mx >>= 1
	return j

def solve(n, a):
	mx = max(a)
	left_bit = mx_2_degree(mx) >> 1
	and_bits = reduce(and_, a)
	or_bits = reduce(or_, a)
	i = left_bit
	while i:
		if or_bits & i != and_bits & i:
			break
		i >>= 1
	if not i:
		return 0
	a.sort(reverse=True)
	for (j, x) in enumerate(a):
		if not x & i:
			break
	mn = min((xor(x, y) for x in a[:j] for y in a[j:]))
	return mn

def enter_data():
	n = int(sys.stdin.readline().strip(), 10)
	a = [int(x.strip(), 10) for x in sys.stdin.readline().split()]
	return solve(n, a)
print(enter_data())


n = int(input())
a = [int(x) for x in input().split()]
bit = 1
for x in a:
	while x > bit:
		bit <<= 1
while bit:
	a0 = []
	a1 = []
	for x in a:
		if x & bit:
			a1.append(x)
		else:
			a0.append(x)
	if a0 and a1:
		small = bit << 1
		for x0 in a0:
			for x1 in a1:
				diff = x0 ^ x1
				if diff < small:
					small = diff
		print(small)
		break
	bit >>= 1
else:
	print(0)


import math
import itertools

def leading_ones(x, highest_bit):
	ones = 0
	cur_bit = highest_bit
	while cur_bit >= 0 and x >> cur_bit & 1 == 1:
		ones += 1
		cur_bit -= 1
	return ones
_ = input()
ns = list(map(int, input().split()))
n_max = max(ns)
if n_max == 0:
	print('0')
	exit()
highest_bit = int(math.log2(n_max))
max_ones = min([leading_ones(n, highest_bit) for n in ns])
shift = highest_bit - max_ones + 1
flag = ~(n_max >> shift << shift)
ns = [n & flag for n in ns]
ones = []
zeros = []
for n in ns:
	if n >> shift - 1 & 1 == 0:
		zeros.append(n)
	else:
		ones.append(n)
l = itertools.product(ones, zeros)
min_score = min([o ^ z for (o, z) in l])
print(min_score)


from functools import reduce
import math

def asplit(arr, bit):
	(a0, a1) = ([], [])
	for x in arr:
		if 1 << bit & x > 0:
			a1.append(x)
		else:
			a0.append(x)
	return (a0, a1)

def min_intersect(a, b, hob):
	if hob == -1:
		return 0
	(a0, a1) = asplit(a, hob)
	(b0, b1) = asplit(b, hob)
	res0 = 99999
	res1 = 99999
	if len(a0) != 0 and len(b0) != 0:
		res0 = min_intersect(a0, b0, hob - 1)
	if len(a1) != 0 and len(b1) != 0:
		res1 = min_intersect(a1, b1, hob - 1)
	m = min(res0, res1)
	if m != 99999:
		return m
	else:
		m = min_intersect(a, b, hob - 1)
		return (1 << hob) + m
n = int(input())
nums = sorted([int(s) for s in input().split(' ')])
an = reduce(lambda x, y: x & y, nums, (1 << 16) - 1)
nums = [i - an for i in nums]
if nums[-1] == 0:
	print(0)
else:
	hob = int(math.log2(nums[-1]))
	(a, b) = asplit(nums, hob)
	print(min_intersect(a, b, hob))


import sys

def solve(ar):
	answer = 0
	byLen = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
	highestNonZeroLen = 0
	uniqueness = False
	latest = 1
	for ni in range(len(ar)):
		n = ar[ni]
		if ni == 0:
			latest = n
		elif n != latest:
			uniqueness = True
		representation = '{0:b}'.format(n)
		replen = len(representation)
		byLen[replen].append(n)
		if replen > highestNonZeroLen:
			highestNonZeroLen = replen
	if uniqueness == False:
		return 0
	numPopulatedLens = 0
	for i in byLen:
		if len(i) > 0:
			numPopulatedLens += 1
	if numPopulatedLens != 1:
		best = 2 ** (highestNonZeroLen - 1)
	else:
		best = 2 ** (highestNonZeroLen - 2)
	worst = 2 ** highestNonZeroLen - 1
	bestSoFar = worst
	for i in byLen[highestNonZeroLen]:
		for n in ar:
			if n ^ i == best:
				return best
			elif best < n ^ i < bestSoFar:
				bestSoFar = n ^ i
	return bestSoFar
n = int(input().strip())
ar = list(map(int, input().strip().split(' ')))
result = solve(ar)
print(result)


def bit_length(k):
	return k.bit_length()

def occurence(arr):
	s = 0
	for i in range(len(arr)):
		if arr[i] == arr[0]:
			s += 1
	return s

def transf(ar, occ):
	if occ == len(ar) and ar[0].bit_length() > 1:
		a = ar[0].bit_length() - 1
		for i in range(occ):
			ar[i] = ar[i] % 2 ** a
		bit_ar = list(map(bit_length, ar))
		occ = occurence(bit_ar)
		transf(ar, occ)
	return occ
n = int(input().strip())
ar = list(map(int, input().strip().split(' ')))
ar.sort(reverse=True)
bit_ar = list(map(bit_length, ar))
occ = occurence(bit_ar)
occ = transf(ar, occ)
if occ != n:
	ar1 = ar[:occ]
	ar2 = ar[occ:]
	arr = []
	for i in range(len(ar1)):
		for j in range(len(ar2)):
			arr.append(ar1[i] ^ ar2[j])
	arr.sort()
	print(arr[0])
elif ar[0] == ar[-1]:
	print(0)
else:
	print(1)


import sys

def length(n):
	i = 0
	while n >= pow(2, i):
		i += 1
	return i

def minmax(a):
	c = []
	for i in range(len(a)):
		if not i:
			c.append([a[i]])
		elif length(a[i]) == length(a[i - 1]):
			c[-1].append(a[i])
		else:
			c.append([a[i]])
	if len(c) == 1:
		a = [temp - pow(2, length(c[0][0]) - 1) for temp in c[0]]
		return minmax(a)
	else:
		ans = c[-1][0] ^ c[0][0]
		for i in c[-1]:
			for j in c[:-1]:
				for k in j:
					ans = min(ans, i ^ k)
		return ans
n = int(input().strip())
a = [int(t) for t in input().strip().split(' ')]
a.sort()
if a[-1] == 0:
	print(0)
else:
	print(minmax(a))


from collections import defaultdict

def count_bits(x):
	count = 0
	while x != 0:
		count += 1
		x >>= 1
	return count

def partition(numbers, bit_count):
	matched = []
	unmatched = []
	for n in numbers:
		if count_bits(n) == bit_count:
			matched.append(n)
		else:
			unmatched.append(n)
	return (matched, unmatched)
n = int(input().strip())
numbers = [int(x) for x in input().strip().split()]
(min_, max_) = (min(numbers), max(numbers))
while min_ != max_ and count_bits(min_) == count_bits(max_):
	numbers = [x ^ 1 << count_bits(max_) - 1 for x in numbers]
	(min_, max_) = (min(numbers), max(numbers))
if min_ == max_:
	print(0)
else:
	(a, b) = partition(numbers, count_bits(max_))
	min_xor = 1 << count_bits(max_)
	for i in a:
		for j in b:
			xor = i ^ j
			if xor < min_xor:
				min_xor = xor
	print(min_xor)


def trim(a):
	to_trim = max_length
	for number in a:
		for i in range(max_length):
			if number[i] == '0':
				to_trim = min(to_trim, i)
				break
	a = list(map(lambda x: x[to_trim:], a))
	return a

def enrich():
	for i in range(len(a)):
		a[i] = '0' * (max_length - len(a[i])) + a[i]
n = int(input())
a = list(map(lambda x: bin(int(x))[2:], input().strip().split(' ')))
max_length = max(map(len, a))
if max_length != min(map(len, a)):
	enrich()
else:
	a = trim(a)
a.sort()
index = 1
for i in range(n):
	if a[i][0] == '1':
		index = i
		break
result = 1000000000000.0
for i in range(index):
	for j in range(index, n):
		result = min(result, int(a[i], 2) ^ int(a[j], 2))
print(result)


from itertools import *
int(input())
a = sorted(list(map(int, input().split())))
if a[0] == a[-1]:
	print(0)
else:
	k = 30
	while not (a[0] ^ a[-1]) >> k & 1:
		k -= 1
print(min((i ^ j for (i, j) in product([i for i in a if i >> k & 1], [i for i in a if not i >> k & 1]))))


def find_interesting_power_of_2(a):
	biggest = max(a)
	res = 1
	bits = 1
	while res < biggest:
		res = res * 2
		bits += 1
	counters = [0] * bits
	for ai in a:
		for p in range(bits):
			if ai & 1 << p != 0:
				counters[p] += 1
	for p in reversed(range(bits)):
		if counters[p] != 0 and counters[p] != len(a):
			return p
	return -1
n = int(input())
a = [int(x) for x in input().split(' ')]
interesting = find_interesting_power_of_2(a)
if interesting == -1:
	print(0)
else:
	left = [x for x in a if x & 1 << interesting > 0]
	right = [x for x in a if x & 1 << interesting == 0]
	final_result = -1
	for l in left:
		for r in right:
			final_result = min(final_result, l ^ r) if final_result != -1 else l ^ r
	print(final_result)


def bit_set(x, b):
	return x & 1 << b != 0

def get_bit_options(arr, b):
	foundZeros = False
	foundOnes = False
	for a in arr:
		if bit_set(a, b):
			foundOnes = True
		else:
			foundZeros = True
		if foundOnes and foundZeros:
			break
	return (foundZeros, foundOnes)

def helper(left, right, b):
	lo = get_bit_options(left, b)
	ro = get_bit_options(right, b)
	if b == 0:
		if lo[0] and ro[0] or (lo[1] and ro[1]):
			return 0
		else:
			return 1
	if lo == ro and (not all(lo)):
		return helper(left, right, b - 1)
	elif lo[0] ^ lo[1] and ro[0] ^ ro[1]:
		return (1 << b) + helper(left, right, b - 1)
	else:
		left0 = [a for a in left if not bit_set(a, b)]
		left1 = [a for a in left if bit_set(a, b)]
		right0 = [a for a in right if not bit_set(a, b)]
		right1 = [a for a in right if bit_set(a, b)]
		result = 1 << b
		if len(left0) > 0 and len(right0) > 0:
			result = helper(left0, right0, b - 1)
		if len(left1) > 0 and len(right1) > 0:
			result = min(result, helper(left1, right1, b - 1))
		return result

def solve(arr):
	startBit = 31
	while startBit >= 0:
		if all(get_bit_options(arr, startBit)):
			break
		startBit -= 1
	else:
		return 0
	left = [a for a in arr if not bit_set(a, startBit)]
	right = [a for a in arr if bit_set(a, startBit)]
	return (1 << startBit) + helper(left, right, startBit - 1)
n = int(input())
arr = list(map(int, input().strip().split()))
print(solve(arr))


(n, a) = (int(input()), list(map(int, input().split())))
(up, down) = ([], [])
while len(down) == 0:
	maxBit = 1 << max([len(bin(i)) for i in a]) - 3
	for i in a:
		if i >= maxBit:
			up.append(i)
		else:
			down.append(i)
	if len(down) == 0:
		for i in range(n):
			a[i] ^= maxBit
minimum = 10000000000
for i in up:
	for j in down:
		minimum = min(minimum, i ^ j)
if len(up) == 0:
	print(0)
else:
	print(minimum)


input()
numlist = [int(num) for num in input().split()]
(big, small) = (numlist[0], numlist[0])
for num in numlist:
	big = max(num, big)
	small = min(num, small)
dist = big ^ small
k = len('{0:b}'.format(dist))
split = big >> k - 1 << k - 1
bignums = []
smallnums = []
for num in numlist:
	if num >= split:
		bignums.append(num)
	else:
		smallnums.append(num)
for num in bignums:
	for othernum in smallnums:
		dist = min(dist, num ^ othernum)
print(dist)


def get_closest_power_2(x):
	if x == 0:
		return 0
	v = 1
	while v <= x:
		v <<= 1
	return v >> 1

def get_mask(x, y):
	assert x >= y
	if y == 0:
		return 0
	p_x = get_closest_power_2(x)
	p_y = get_closest_power_2(y)
	assert p_x > 0 and p_y > 0
	if p_x != p_y:
		return 0
	else:
		mask = 0
		cur_mask = p_y
		while cur_mask & y > 0:
			mask |= cur_mask
			cur_mask >>= 1
		return mask

def max_xor(xs):
	flag = get_closest_power_2(max(xs))
	ones = []
	zeros = []
	for x in xs:
		if x & flag > 0:
			ones.append(x)
		else:
			zeros.append(x)
	if len(ones) == 0:
		return 0
	best = 10 ** 10
	for one in ones:
		for zero in zeros:
			v = one ^ zero
			if v < best:
				best = v
	return best
n = int(input())
xs = list(map(int, input().split()))
xs = sorted(xs, reverse=True)
(x, y) = (xs[0], xs[-1])
m = get_mask(x, y)
print(max_xor([m ^ v for v in xs]))


import math
import sys

def find_min_max(a):
	if all((x == 0 for x in a)):
		return 0
	max2 = 2 ** int(math.log2(max(a)))
	less = [e for e in a if e < max2]
	more = [e for e in a if e >= max2]
	if less and more:
		return min((x ^ y for x in less for y in more))
	else:
		new_a = [max2 ^ x for x in a]
		return find_min_max(new_a)
n = int(input())
a = [int(temp) for temp in input().split()]
print(find_min_max(a))


n = int(input())
arr = list(map(int, input().strip().split()))
mx = -1
(mxarr, mnarr) = ([], [])
for x in arr:
	l = x.bit_length() - 1
	if l > mx:
		mxarr = [x]
		mx = l
	elif l == mx:
		mxarr.append(x)
fil = list(filter(lambda x: x not in mxarr, arr))
anarr = []
if len(fil) == 0:
	print(mxarr[0] ^ mxarr[1])
else:
	for x in mxarr:
		for y in fil:
			anarr.append(x ^ y)
	print(min(anarr))


import sys

def permutation_gen(aa):
	stack = [([], aa)]
	while stack:
		(p, rest) = stack.pop()
		if not rest:
			yield p
		else:
			for (i, r) in enumerate(rest):
				stack.append((p + [r], rest[:i] + rest[i + 1:]))

def score(aa):
	rval = None
	for (a, b) in zip(aa, aa[1:]):
		x = a ^ b
		if not rval or rval < x:
			rval = x
	return rval

def solve_bf(aa):
	print('aa=%s' % aa, file=sys.stderr)
	rval = None
	for p in permutation_gen(aa):
		s = score(p)
		print('s=%d p=%s' % (s, p), file=sys.stderr)
		if rval is None or rval > s:
			rval = s
	return rval

def solve(aa):

	def rbin_gen(a, width):
		while a:
			yield (a % 2)
			width -= 1
			a //= 2
		while width > 0:
			yield 0
			width -= 1
	print('aa=%s' % aa, file=sys.stderr)
	mrb = list(rbin_gen(max(aa), 0))
	width = len(mrb)
	bb = [tuple(rbin_gen(a, width))[::-1] for a in aa]
	print('bb=%s' % bb, file=sys.stderr)
	(lii, position) = (None, 0)
	while position != width:
		lii = [i for (i, b) in enumerate(bb) if b[position] == 1]
		if len(lii) < len(bb):
			break
		position += 1
	print('position=%d lii=%s' % (position, lii), file=sys.stderr)
	if position == width:
		return 0
	slii = set(lii)
	rii = [i for i in range(len(bb)) if i not in slii]
	print('rii=%s' % rii, file=sys.stderr)
	rval = None
	for i in lii:
		a = aa[i]
		for j in rii:
			b = aa[j]
			r = a ^ b
			if rval is None or rval > r:
				rval = r
	return rval
_ = int(input())
aa = list(map(int, input().split()))
print(solve(aa))


def binary(a):
	x = ''
	while a > 0:
		x = str(a % 2) + x
		a //= 2
	return x

def update(x):
	x = x[1:]
	i = 0
	while i < len(x) and x[i] != '1':
		i += 1
	x = x[i:]
	return x
n = int(input())
a = list(map(int, input().split()))
lengths = []
binary_a = {}
for i in range(len(a)):
	x = binary(a[i])
	if len(x) in binary_a:
		binary_a[len(x)].append(a[i])
	else:
		binary_a[len(x)] = [a[i]]
		lengths.append(len(x))
max_length = max(lengths)
min_xor = float('inf')
if max_length == 0:
	print('0')
	exit()
while len(binary_a) == 1:
	lengths = []
	for i in binary_a[max_length]:
		x = update(binary(i))
		if len(x) in binary_a:
			binary_a[len(x)].append(i)
		else:
			binary_a[len(x)] = [i]
			lengths.append(len(x))
	binary_a.pop(max_length)
	max_length = max(lengths)
for i in binary_a[max_length]:
	for j in binary_a:
		if j != max_length:
			for k in binary_a[j]:
				min_xor = min(min_xor, i ^ k)
print(min_xor)


n = int(input())
ns = [int(x) for x in input().split()]
res = 0
for x in ns:
	for y in ns:
		res = max(res, x ^ y)
bit = 1 << len(bin(res)[3:])
A = [x for x in ns if x & bit]
B = [x for x in ns if x & bit == 0]
for x in A:
	for y in B:
		res = min(res, x ^ y)
print(res)


def bitize(n, bits=32):
	max_bit = -1
	L = [0 for i in range(0, bits)]
	if n == 0:
		return [L, -1]
	for i in range(0, bits):
		if n == 0 and max_bit == -1:
			max_bit = i - 1
		L[i] = n % 2
		n //= 2
	return [L, max_bit]

def find_min_max(A):
	BA = [bitize(a) for a in A]
	max_bit = -1
	for i in range(0, len(BA)):
		if BA[i][1] > max_bit:
			max_bit = BA[i][1]
	max_list = []
	less_list = []
	top_A = []
	if max_bit == -1:
		return 0
	for j in range(0, len(BA)):
		if BA[j][1] < max_bit:
			less_list.append(A[j])
		elif BA[j][1] == max_bit:
			max_list.append(A[j])
			top_A.append(A[j] ^ pow(2, max_bit))
	min_val = pow(2, 32)
	if len(max_list) > 0 and len(less_list) > 0:
		for i in range(0, len(max_list)):
			for j in range(0, len(less_list)):
				if min_val > max_list[i] ^ less_list[j]:
					min_val = max_list[i] ^ less_list[j]
		return min_val
	elif len(less_list) == 0:
		counts = [0 for i in range(0, len(top_A))]
		if len(max_list) % 2 == 0:
			for i in range(0, len(top_A)):
				top_A[i] += pow(2, max_index)
				counts[i] = find_min_max(top_A)
				top_A[i] -= pow(2, max_index)
			return min(counts)
		else:
			return find_min_max(top_A)
		return min_val
n = int(input())
A = [int(a) for a in input().split()]
print(find_min_max(A))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys
from math import inf

def dec_to_bin(number):
	return bin(number)[2:]

def bin_to_dec(number):
	return int(number, 2)

def buildup_to_len(number, length):
	return '0' * (length - len(number)) + number

def get_max_len(arr):
	return len(dec_to_bin(max(arr)))

def get_min_len(arr):
	return len(dec_to_bin(min(arr)))

def anotherMinimaxProblem(a):
	res = inf
	arr_bin = []
	if max(a) == min(a) and max(a) == 0:
		return 0
	for el in a:
		arr_bin.append(dec_to_bin(el))
	while len(max(arr_bin, key=len)) == len(min(arr_bin, key=len)):
		arr_bin = [el[:1].lstrip('1') + el[1:] for el in arr_bin]
		arr_bin = [el.lstrip('0') for el in arr_bin]
	max_len = len(max(arr_bin, key=len))
	arr_bin = [buildup_to_len(el, max_len) for el in arr_bin]
	arr_zeros = []
	arr_ones = []
	for el in sorted(arr_bin):
		if el[0] == '0':
			arr_zeros.append(el)
		else:
			arr_ones.append(el)
	for el_z in arr_zeros:
		for el_o in arr_ones:
			res = min(res, bin_to_dec(el_z) ^ bin_to_dec(el_o))
	return res
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


n = int(input())
a = list(map(int, input().split()))
try:
	c = 1 << max(a).bit_length() - 1
	while min(a) >= c:
		a = [i - c for i in a]
		c = 1 << max(a).bit_length() - 1
	print(min((i ^ j for i in a for j in a if i < c and j >= c)))
except:
	print(0)


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def anotherMinimaxProblem(a):
	a.sort()
	t = 0
	while 2 ** t <= a[-1]:
		t += 1
	ander = ['1' for x in range(t)]
	if ander == []:
		return 0
	ander = int(''.join(ander), 2)
	ander2 = ander
	for i in a:
		ander = ander & i
	ander2 = ander2 ^ ander
	tt = 0
	while 2 ** tt < ander2:
		tt += 1
	tt = tt - 1
	seta = []
	setb = []
	x = int(2 ** tt)
	lap = int(2 ** (tt + 1))
	for i in a:
		if i % lap >= x:
			seta.append(i)
		else:
			setb.append(i)
	minimum = seta[0] ^ setb[0]
	for i in seta:
		for j in setb:
			temp = i ^ j
			if temp < minimum:
				minimum = temp
	return minimum
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import sys
from math import log2

def findABsets(a):
	leading_ones = min(a) ^ max(a)
	b = 1
	while leading_ones:
		leading_ones >>= 1
		b <<= 1
	b >>= 1
	A = []
	B = []
	for el in a:
		if el & b:
			B.append(el)
		else:
			A.append(el)
	return (A, B)

def anotherMinimaxProblem(a):
	(A, B) = findABsets(a)
	if len(A) > 0 and len(B) > 0:
		return min([a_el ^ b_el for a_el in A for b_el in B])
	else:
		return 0
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def anotherMinimaxProblem(a):
	msb1 = []
	msb0 = []
	shifter = 32
	allinone = 1
	while allinone and shifter > 1:
		shifter -= 1
		msb1 = []
		msb0 = []
		for i in range(len(a)):
			sh = a[i] >> shifter
			if sh & 1 == 1:
				msb1.append(a[i])
			else:
				msb0.append(a[i])
		if len(msb1) != 0 and len(msb0) != 0:
			allinone = 0
	if len(msb1) == 0 or len(msb0) == 0:
		return 0
	mn = 2 ** 32
	for i in range(len(msb1)):
		for j in range(len(msb0)):
			mn = min(mn, msb1[i] ^ msb0[j])
	return mn
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


from math import log, pow

def findBiggest(A, msbNum):
	i = 0
	biggestArr = []
	while i < len(A):
		if A[i] & msbNum:
			biggestArr.append(A[i])
		else:
			break
		i += 1
	return biggestArr

def findMin(sA, sB):
	minimax = int(pow(10, 9))
	for el1 in sA:
		for el2 in sB:
			minimax = min(minimax, el1 ^ el2)
	return minimax

def main():
	n = int(input().rstrip())
	bins = [n - 1 for i in range(n)]
	A = list(map(int, input().rstrip().split()))
	A.sort(reverse=True)
	minimax = 0
	msb = 0
	while A[0] > 0:
		msb = int(log(A[0], 2))
		msbNum = 1 << msb
		biggestArr = findBiggest(A, msbNum)
		if len(biggestArr) == n:
			for i in range(n):
				A[i] ^= msbNum
		else:
			break
	if A[0] > 0:
		sA = set(biggestArr)
		sB = set(A) - sA
		minimax = findMin(sA, sB)
	print(minimax)
main()


import sys
import functools

def bit_length(x):
	pos = 0
	while x >> pos > 0:
		pos += 1
	return pos

def xor_cmp(a, b):
	max_pos = max(bit_length(a), bit_length(b))
	while max_pos >= 0:
		if a >> max_pos > 0 and b >> max_pos > 0:
			max_pos -= 1
			if max_pos >= 0:
				a -= 1 << max_pos
				b -= 1 << max_pos
			continue
		if a >> max_pos == 0 and b >> max_pos == 0:
			max_pos -= 1
			continue
		if a >> max_pos == 0 and b >> max_pos > 0:
			return -1
		if a >> max_pos > 0 and b >> max_pos == 0:
			return 1
	return 0

def anotherMinimaxProblem(a):
	max_bit_length = 0
	for x in a:
		if bit_length(x) > max_bit_length:
			max_bit_length = bit_length(x)
	pos = max_bit_length - 1
	if pos == 0:
		return 0
	while pos >= 0:
		stop = False
		for x in a:
			if x & 1 << pos == 0:
				stop = True
		if stop:
			break
		pos -= 1
	if pos < 0:
		return 0
	xs = [x for x in a if x & 1 << pos]
	ys = [y for y in a if y & 1 << pos == 0]
	return min((x ^ y for x in xs for y in ys))
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def anotherMinimaxProblem(a, n):
	if all((e == 0 for e in a)):
		return 0
	digit_keys = {}
	for elem in a:
		count = 0
		while elem > 0:
			one = elem % 2
			if one == 1:
				digit_keys[count] = digit_keys.get(count, 0) + 1
			elem //= 2
			count += 1
	max_digit = 0
	for key in digit_keys:
		if digit_keys[key] != n:
			max_digit = max(max_digit, key)
	withZero = []
	withOne = []
	digit = 2 ** max_digit
	for elem in a:
		if elem & digit == 0:
			withZero.append(elem)
		else:
			withOne.append(elem)
	minXor = 10 ** 9
	for e1 in withZero:
		for e2 in withOne:
			minXor = min(minXor, e1 ^ e2)
	return minXor
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a, n)
print(result)


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def anotherMinimaxProblem(a):
	a_min = min(a)
	a_max = max(a)
	if a_min == a_max:
		return 0
	a_min_bin = bin(a_min)
	a_max_bin = bin(a_max)
	if len(a_min_bin) < len(a_max_bin):
		barrier = 2 ** (len(a_max_bin) - 3)
	else:
		for i in range(2, len(a_min_bin)):
			if a_min_bin[i] != a_max_bin[i]:
				barrier = int(a_max_bin[:i + 1] + '0' * (len(a_min_bin) - i - 1), 2)
				break
	lower = []
	upper = []
	for num in a:
		if num < barrier:
			lower.append(num)
		else:
			upper.append(num)
	minimax = barrier * 2
	for num1 in lower:
		for num2 in upper:
			current_xor = num1 ^ num2
			if current_xor < minimax:
				minimax = current_xor
	return minimax
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import itertools
import sys

def anotherMinimaxProblem(a):
	bin_l = list(map(lambda x: bin(x)[2:], a))
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import sys
from math import log

def anotherMinimaxProblem(a):
	b = list(map(lambda x: bin(x)[2:], a))
	l = set([len(x) for x in b])
	if 0 in l or b[0][0] == '0':
		return 0
	while len(l) == 1:
		b = [bin(int(x[1:], 2))[2:] for x in b]
		l = set([len(x) for x in b])
		if 0 in l or b[0][0] == '0':
			return 0
	ml = max(l)
	c = set([int(x, 2) for x in b if len(x) == ml])
	d = set([int(x, 2) for x in b if len(x) != ml])
	return min([x ^ y for x in c for y in d])
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


(n, data, r) = (int(input()), [int(x) for x in input().split(' ')], 0)

def cal1(hbuf, lbuf):
	if len(hbuf) == 1 and len(lbuf) == 1:
		return list(hbuf)[0] ^ list(lbuf)[0]
	(mm, ms) = (max(max(hbuf), max(lbuf)), 1)
	while ms <= mm:
		ms += ms
	mh = ms >> 1
	lz1 = set([y - mh for y in filter(lambda x: x & mh, hbuf)])
	lz2 = set([x for x in filter(lambda x: not x & mh, hbuf)])
	lv1 = set([x - mh for x in filter(lambda x: x & mh, lbuf)])
	lv2 = set([x for x in filter(lambda x: not x & mh, lbuf)])
	(c1, c2) = (None, None)
	if len(lz1) and len(lv1):
		c1 = cal1(lz1, lv1)
	if len(lz2) and len(lv2):
		c2 = cal1(lz2, lv2)
	if c1 == None:
		c = c2
	elif c2 == None:
		c = c1
	else:
		c = min(c1, c2)
	if c == None:
		c = cal1(lz1 if len(lz1) else lz2, lv1 if len(lv1) else lv2) + mh
	return c + r
while True:
	(mm, ms) = (max(data), 1)
	if mm == 0:
		print(0)
		break
	while ms <= mm:
		ms += ms
	ms >>= 1
	s1 = set(map(lambda x: x - ms, filter(lambda x: x >= ms, data)))
	s2 = set(filter(lambda x: x < ms, data))
	if not s2:
		data = [x - ms for x in data]
		continue
	print(cal1(s1, s2) + ms)
	break


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


import sys

def anotherMinimaxProblem(a):
	bin_list = list(map(lambda x: bin(x)[2:], a))
	while True:
		len_set = set(map(lambda x: len(x), bin_list))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_list[0][0] == '0':
			return 0
		bin_list = list(map(lambda x: bin(int(x[1:], 2)), bin_list))
	max_len = max(map(lambda x: len(x), bin_list))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_list)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_list)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
result = anotherMinimaxProblem(a)
print(result)


import math
import os
import random
import re
import sys

def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


n = int(input())
a = list(map(int, input().rstrip().split()))
if len(set(a)) == 1 and list(set(a))[0] == 0:
	print(0)
else:
	bitor = bitnot = 0
	for number in a:
		bitor |= number
		bitnot |= ~number
	i = bitor & bitnot
	while i & i - 1:
		i &= i - 1
	xs = [number for number in a if number & i]
	ys = [number for number in a if number & i == 0]
	print(min((x ^ y for x in xs for y in ys)))


from math import log
from itertools import product

def lg(x):
	return int(log(x, 2)) + 1 if x > 0 else 0
N = input()
A = [int(x) for x in input().split()]
max_2 = max(map(lg, A))
(P2, P) = (set(), set())
for a in A:
	P2.add(a) if lg(a) == max_2 else P.add(a)
if P:
	p2 = P2.pop()
	min_xor = min((p2 ^ p for p in P))
	for (p2, p) in product(P2, P):
		min_xor = min(p2 ^ p, min_xor)
	print(min_xor)
elif len(P2) == 1:
	print(0)
else:
	p2_min = min(P2)
	pp2 = bin(p2_min)[2:]
	k = 0
	for (i, s) in enumerate(pp2):
		if s == '1':
			k = 2 * k + 1
		else:
			break
	k *= 2 ** (len(pp2) - i)
	P3 = set(map(lambda x: x - k, P2))
	max_2 = max(map(lg, P3))
	P2 = set()
	for p3 in P3:
		P2.add(p3) if lg(p3) == max_2 else P.add(p3)
	p2 = P2.pop()
	min_xor = min((p2 ^ p for p in P))
	for (p2, p) in product(P2, P):
		min_xor = min(p2 ^ p, min_xor)
	print(min_xor)


from math import log
from itertools import product

def lg(x):
	return int(log(x, 2)) + 1 if x > 0 else 0
N = input()
A = [int(x) for x in input().split()]
max_2 = max(map(lg, A))
(P2, P) = (set(), set())
for a in A:
	P2.add(a) if lg(a) == max_2 else P.add(a)
if P:
	p2 = P2.pop()
	min_xor = min((p2 ^ p for p in P))
	for (p2, p) in product(P2, P):
		min_xor = min(p2 ^ p, min_xor)
	print(min_xor)
elif len(P2) == 1:
	print(0)
else:
	p2_min = min(P2)
	pp2 = bin(p2_min)[2:]
	k = 0
	for (i, s) in enumerate(pp2):
		if s == '1':
			k = 2 * k + 1
		else:
			break
	k *= 2 ** (len(pp2) - i)
	P3 = set(map(lambda x: x - k, P2))
	max_2 = max(map(lg, P3))
	P2 = set()
	for p3 in P3:
		P2.add(p3) if lg(p3) == max_2 else P.add(p3)
	p2 = P2.pop()
	min_xor = min((p2 ^ p for p in P))
	for (p2, p) in product(P2, P):
		min_xor = min(p2 ^ p, min_xor)
	print(min_xor)


n = int(input())
a = list(map(int, input().split()))
ind = 0
x = max(a)
while x:
	ind += 1
	x >>= 1
d = {}
for i in range(ind + 1):
	d[i] = []
	for el in a:
		if el & 1 << i:
			d[i].append(el)
while ind >= 0 and len(d[ind]) == n or len(d[ind]) == 0:
	ind -= 1
	if ind < 0:
		break
res = 0
if ind >= 0:
	res = 10000000000
	good = set()
	for el in d[ind]:
		good.add(el)
	for el in d[ind]:
		for num in a:
			if not num in good:
				res = min(res, el ^ num)
print(res)


import math
import os
import random
import re
import sys

def anotherMinimaxProblem(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(anotherMinimaxProblem(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))


def find_answer(bin_l):
	while True:
		len_set = set(map(lambda x: len(x), bin_l))
		if len(len_set) > 1:
			break
		if 0 in len_set or bin_l[0][0] == '0':
			return 0
		bin_l = list(map(lambda x: bin(int(x[1:], 2)), bin_l))
	max_len = max(map(lambda x: len(x), bin_l))
	big_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) == max_len, bin_l)))
	small_set = set(map(lambda x: int(x, 2), filter(lambda x: len(x) != max_len, bin_l)))
	return min([cur_big ^ cur_small for cur_big in big_set for cur_small in small_set])
input()
l = list(map(int, input().split()))
bin_list = list(map(lambda x: bin(x)[2:], l))
print(find_answer(bin_list))