Boboniu likes bit operations. He wants to play a game with you.

Boboniu gives you two sequences of non-negative integers $a_1,a_2,\ldots,a_n$ and $b_1,b_2,\ldots,b_m$.

For each $i$ ($1\le i\le n$), you're asked to choose a $j$ ($1\le j\le m$) and let $c_i=a_i\& b_j$, where $\&$ denotes the bitwise AND operation. Note that you can pick the same $j$ for different $i$'s.

Find the minimum possible $c_1 | c_2 | \ldots | c_n$, where $|$ denotes the bitwise OR operation.


-----Input-----

The first line contains two integers $n$ and $m$ ($1\le n,m\le 200$).

The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0\le a_i < 2^9$).

The next line contains $m$ integers $b_1,b_2,\ldots,b_m$ ($0\le b_i < 2^9$).


-----Output-----

Print one integer: the minimum possible $c_1 | c_2 | \ldots | c_n$.


-----Examples-----
Input
4 2
2 6 4 0
2 4

Output
2
Input
7 6
1 9 1 9 8 1 0
1 1 4 5 1 4

Output
0
Input
8 5
179 261 432 162 82 43 10 38
379 357 202 184 197

Output
147


-----Note-----

For the first example, we have $c_1=a_1\& b_2=0$, $c_2=a_2\& b_1=2$, $c_3=a_3\& b_1=0$, $c_4 = a_4\& b_1=0$.Thus $c_1 | c_2 | c_3 |c_4 =2$, and this is the minimal answer we can get.

Difficulty: MEDIUM_HARD

Solutions:
(n, m) = map(int, input().split())
arrA = list(map(int, input().split()))
arrB = list(map(int, input().split()))
ans = 511
for val in range(510, -1, -1):
	temp = True
	for a in arrA:
		for b in arrB:
			if a & b | val == val:
				break
		else:
			temp = False
			break
	if temp:
		ans = val
print(ans)


input()
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(513):
	for j in A:
		f = 0
		for k in B:
			if j & k | i == i:
				f = 1
		if f != 1:
			break
	else:
		break
print(i)


import math
from decimal import Decimal
import heapq
import copy
import heapq
from collections import deque
from collections import defaultdict
MOD = 1000000007

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def da():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, m, a)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(n):
	tc = []
	ans = {}
	d = 2
	while d * d <= n:
		if n % d == 0:
			tc.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		tc.append(n)
	for i in tc:
		ans[i] = ans.get(i, 0) + 1
	return ans

def to_zero(n, s):
	lst = s[0]
	ans = []
	for i in range(n):
		if lst == s[i]:
			continue
		elif lst == '0':
			lst = '1'
			ans.append(i)
		else:
			lst = '0'
			ans.append(i)
	if lst == '1':
		ans.append(n)
	return ans
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for x in range(513):
	ff = True
	for i in a:
		f = False
		for j in b:
			if i & j | x == x:
				f = True
				break
		if not f:
			ff = False
			break
	if ff:
		print(x)
		exit()


ip = input().split(' ')
n = ip[0]
m = ip[1]
a = [int(i) for i in input().split(' ')]
b = [int(i) for i in input().split(' ')]
c = []
for ai in a:
	c.append([])
	for bi in b:
		c[-1].append(ai & bi)
for i in range(0, len(c)):
	for j in range(0, len(c[i])):
		num = c[i][j]
		c[i][j] = ''
		for t in range(0, 9):
			c[i][j] += str(num % 2)
			num //= 2
		c[i][j] = c[i][j][::-1]
o = 0
for i in range(0, 9):
	d = []
	f = 0
	for cc in c:
		d.append([])
		for e in cc:
			if e[i] == '0':
				d[-1].append(e)
		if len(d[-1]) == 0:
			d[-1] = cc
			f = 1
	o *= 2
	o += f
	if f == 0:
		c = d
print(o)


(n, m) = map(int, input().split())
arr = [int(o) for o in input().split()]
barr = [int(o) for o in input().split()]
for i in range(2 ** 9 + 1):
	co = 0
	for a in arr:
		flag = 0
		for b in barr:
			if a & b & i == a & b:
				flag = 1
				break
		if flag == 1:
			co += 1
	if co == n:
		print(i)
		break


import sys

def main():
	(n, m) = Imap()
	a = Ilist()
	b = Ilist()
	arr = [[0] * m for i in range(n)]
	for i in range(n):
		ai = a[i]
		for j in range(m):
			arr[i][j] = ai & b[j]
	for k in range(1 << 9):
		for i in range(n):
			for j in range(m):
				if k | arr[i][j] == k:
					break
			else:
				break
		else:
			print(k)
			break
FIO = 10

def I():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
en = enumerate

def answer_debug(*args):
	print('\x1b[31m', *args, '\x1b[0m', file=sys.stderr)

def debug(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
for _ in range(1):
	raj = 9
	mms = set()
	vini = defaultdict(int)
	(n, m) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	d = defaultdict(list)
	pii = defaultdict(list)
	nut = set(l1)
	for i in range(m):
		c = bin(l2[i])[2:]
		c = '0' * (9 - len(c)) + c
		for j in range(9):
			if c[j] == '0':
				d[9 - j].append(l2[i])
	for i in range(n):
		cc = bin(l1[i])[2:]
		cc = '0' * (9 - len(cc)) + cc
		for j in range(9):
			if cc[j] == '1':
				pii[9 - j].append(l1[i])
	cur = l2 + []
	ans = 0
	nl = list()
	nl.append(ans)
	nv = 98
	v = 9
	resul = set()
	for i in range(9, 0, -1):
		if len(pii[i]) == 0:
			continue
		else:
			for j in pii[i]:
				e = set(l2)
				c = bin(j)[2:]
				c = '0' * (9 - len(c)) + c
				for k in range(9, i - 1, -1):
					if c[9 - k] == '1' and k not in resul:
						e = e & set(d[k])
				if len(e) == 0:
					ans += 2 ** (i - 1)
					resul.add(i)
					break
	print(ans + 98 - nv)


import sys
input = sys.stdin.buffer.readline
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
pa = [[-1 for __ in range(m)] for _ in range(n)]
for i in range(n):
	for j in range(m):
		pa[i][j] = a[i] & b[j]
memo = [[False for __ in range(2 ** 9)] for _ in range(n)]
for i in range(n):
	for j in range(m):
		if i == 0:
			memo[i][pa[i][j]] = True
		else:
			for k in range(2 ** 9):
				if memo[i - 1][k]:
					ans = k | pa[i][j]
					memo[i][ans] = True
for k in range(2 ** 9):
	if memo[n - 1][k]:
		print(k)
		break


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = 0
for i in range(0, 513):
	c = 1
	for j in a:
		u = 0
		for k in b:
			if i | j & k == i:
				u = 1
				break
		if u == 0:
			c = 0
			break
	if c == 1:
		print(i)
		break


data = [*open(0)]
dt = lambda x: map(int, data[x].split())
a = (*dt(1),)
b = (*dt(2),)
for k in range(512):
	for ai in a:
		flag = False
		for bi in b:
			if k | ai & bi == k:
				flag = True
				break
		if not flag:
			break
	else:
		break
print(k)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
from math import gcd
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
(n, m) = Ri()
a = Ri()
b = Ri()
ans = 0
for l in range(2 ** 9 + 1):
	flag = True
	for i in range(n):
		tflag = False
		for j in range(m):
			if a[i] & b[j] | l == l:
				tflag = True
				break
		if not tflag:
			flag = False
			break
	if flag:
		ans = l
		break
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for num in range(512):
	for i in range(n):
		ans = 0
		for j in range(m):
			ans |= a[i] & b[j] | num == num
		if ans == 0:
			break
	if ans == 1:
		print(num)
		break


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = [int(item) for item in input().split()]
b = [int(item) for item in input().split()]
for target in range(2 ** 9):
	mask = ~target
	all_ok = True
	for ai in a:
		ok = False
		for bi in b:
			if not ai & bi & mask:
				ok = True
				break
		if not ok:
			all_ok = False
			break
	if all_ok:
		print(target)
		exit()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
flag = 0
well = 0
good = 0
while flag == 0:
	for i in range(n):
		well = 0
		for j in range(m):
			c = a[i] & b[j]
			if c | ans == ans:
				well = 1
				good += 1
				break
		if well == 0:
			ans += 1
			good = 0
			break
	if good == n:
		flag = 1
print(ans)


import sys
import math
from collections import defaultdict
import heapq
t = 1
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	ans = 0
	cur = 1 << 10
	vis = [0 for x in range(n)]
	for i in range(n):
		maxx = 0
		ind = -1
		ind1 = -1
		for i in range(n):
			minn = 10000000000.0
			if vis[i] == 0:
				for j in range(m):
					y = (a[i] & b[j] | cur) - cur
					if y < minn:
						minn = y
				if maxx < minn:
					maxx = minn
					ind = i
		if cur == 1 << 10:
			cur = int(maxx)
		else:
			cur += maxx
		vis[ind] = 1
	cur %= 1 << 10
	print(cur)


import sys
input = sys.stdin.readline
for _ in range(1):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for k in range(0, 2 ** 9):
		ct = 0
		for i in range(n):
			fl = 0
			for j in range(m):
				if a[i] & b[j] | k == k:
					ct += 1
					fl = 1
					break
			if fl == 0:
				break
		if ct == n:
			print(k)
			break


import sys
import math
from math import inf, gcd
from heapq import *
from functools import *
from itertools import *
from collections import *
from typing import *
sys.setrecursionlimit(10 ** 5)
mod = 10 ** 9 + 7
(n, m) = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
for ans in range(0, 1 << 9):
	flag = True
	for i in a:
		f = False
		for j in b:
			if i & j | ans == ans:
				f = True
				break
		if not f:
			flag = False
			break
	if flag:
		print(ans)
		break


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
(n, m) = MI()
a = LI()
b = LI()
for v in range(2 ** 10):
	cnt = 0
	for i in range(n):
		for j in range(m):
			if a[i] & b[j] | v == v:
				cnt += 1
				break
	if cnt == n:
		break
print(v)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(513):
	for j in range(n):
		flag = False
		for k in range(m):
			if a[j] & b[k] | i == i:
				flag = True
				break
		else:
			break
	else:
		print(i)
		exit()


(n, m) = input().strip().split()
(n, m) = (int(n), int(m))
a = input().strip().split()
a = [int(x) for x in a]
b = input().strip().split()
b = [int(x) for x in b]
max_val = 1 << 9
true_ans = 1 << 9
for ans in range(max_val):
	cnt = 0
	for i in a:
		for j in b:
			if ans | i & j == ans:
				cnt += 1
				break
	if cnt == len(a):
		true_ans = ans
		break
print(true_ans)


(n, m) = map(int, input().split())
l = [int(x) for x in input().split()]
r = [int(x) for x in input().split()]
for ans in range(512):
	flag = True
	for i in range(n):
		flag1 = False
		for j in range(m):
			if l[i] & r[j] | ans == ans:
				flag1 = True
				break
		if flag1 == False:
			flag = False
			break
	if flag == True:
		print(ans)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [[0] * 2 ** 9 for i in range(n + 1)]
dp[0][0] = 1
for i in range(n):
	for j in range(m):
		for k in range(2 ** 9):
			if dp[i][k]:
				dp[i + 1][k | a[i] & b[j]] = 1
for i in range(2 ** 9):
	if dp[n][i]:
		print(i)
		exit()


from sys import stdin
(n, m) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
c = []
for i in a:
	d = []
	for j in b:
		d.append(i & j)
	c.append(d)
d = [False] * 515
d[0] = True
e = [False] * 515
for i in c:
	for j in range(515):
		if d[j]:
			for k in i:
				e[j | k] = True
	for j in range(515):
		d[j] = e[j]
	for j in range(515):
		e[j] = False
for i in range(515):
	if d[i]:
		print(i)
		break


(n, m) = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
val = 2 ** 9
a_and_b = []
for i in a:
	ai = []
	for j in b:
		ai.append(i & j)
	a_and_b.append(ai)
ans = val
for i in range(val):
	cnt = 0
	for j in a_and_b:
		for k in j:
			if k | i == i:
				cnt += 1
				break
		else:
			break
	if cnt == n:
		ans = min(ans, i)
print(ans)


def check(n, m, x):
	for i in range(n):
		flag = False
		for j in range(m):
			res = a[i] & b[j]
			if res | x == x:
				flag = True
				break
		if flag is False:
			return False
	return True

def solve(n, m, a, b):
	for x in range(0, 1 << 9):
		if check(n, m, x):
			print(x)
			return
		else:
			continue
	return
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
solve(n, m, a, b)


def go(res):
	cur = 0
	for i in range(n):
		for j in range(m):
			f = a[i] & b[j]
			if res | f == res:
				cur |= f
				break
		else:
			return False
	return cur == res
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(1 << 9):
	if go(i):
		exit(print(i))


(n, m) = list(map(int, input().split()))
a_nums = list(map(int, input().split()))
b_nums = list(map(int, input().split()))
cand = set([0])
for a in a_nums:
	new_cand = set([])
	for b in b_nums:
		for c in cand:
			new_cand.add(c | a & b)
	cand = new_cand
print(min(cand))


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
best = 99999999
helpers = []
for i in range(1, 9):
	c = 0
	for j in range(i, -1, -1):
		c += 2 ** j
		helpers.append(c)
helpers = [i for i in range(512)]
for helper in helpers:
	c = []
	cnt = 0
	for i in range(n):
		ok = 0
		for j in range(m):
			x = helper | a[i] & b[j]
			if x <= helper:
				ok = 1
				break
		if ok == 1:
			cnt += 1
	if cnt == len(a):
		ans = helper
		break
print(ans)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
t = 1
for _ in range(t):
	(n, m) = inp()
	a = list(inp())
	b = list(inp())
	ans = float('inf')
	total = 1 << 9
	for check in range(total):
		flag = True
		for i in a:
			ch = False
			for j in b:
				if i & j | check == check:
					ch = True
			if ch:
				pass
			else:
				flag = False
		if flag:
			ans = min(ans, check)
	print(ans)


from math import floor, log2

def find_min_or(a_seq, b_seq):
	bits_n = max(map(_get_number_bitlength, a_seq))
	success_bits = []
	best_b_selection = None
	for bit_index in reversed(range(bits_n)):
		b_selection = _try_exclude_bit(a_seq, b_seq, bit_index, success_bits)
		if b_selection is not None:
			best_b_selection = b_selection
			success_bits.append(bit_index)
	if best_b_selection is None:
		best_b_selection = [b_seq[0]] * len(a_seq)
	return _compute_or(a_seq, best_b_selection)

def _try_exclude_bit(a_seq, b_seq, bit_index, bits_successful):
	selected_b = [None] * len(a_seq)
	for (i_a, a) in enumerate(a_seq):
		for b in b_seq:
			if _get_bit_by_index(a & b, bit_index) == 0:
				if all((_get_bit_by_index(a & b, bit_index) == 0 for bit_index in bits_successful)):
					selected_b[i_a] = b
					break
	if None in selected_b:
		return None
	return selected_b

def _compute_or(a_seq, selected_b):
	min_value = 0
	for (a, b) in zip(a_seq, selected_b):
		min_value |= a & b
	return min_value

def _get_number_bitlength(num):
	result = floor(log2(num)) + 1 if num != 0 else 1
	return result

def _get_bit_by_index(num, i):
	return num >> i & 1

def main():
	(n, m) = map(int, input().split())
	a_seq = tuple(map(int, input().split()))
	b_seq = tuple(map(int, input().split()))
	min_value = find_min_or(a_seq, b_seq)
	print(min_value)
main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
out = 0
while out < 512:
	f = False
	for i in a:
		f = False
		for j in b:
			if i & j | out <= out:
				f = True
				break
		if not f:
			break
	if f:
		print(out)
		break
	out += 1


def ans(a, b, i, n, m):
	if i == n:
		return 0
	d = ans(a, b, i + 1, n, m)
	x = a[i] & b[0] | d
	for j in range(1, m):
		x = min(x, a[i] & b[j] | d)
	return x
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = []
for i in a:
	c = []
	for j in b:
		c.append('{0:09b}'.format(i & j))
	k.append(c)
c = ''
for i in range(9):
	d = 0
	for j in k:
		for b in j:
			if b[i] == '0':
				d += 1
				break
	if d == n:
		c += '0'
		for j in range(n):
			e = []
			for b in k[j]:
				if b[i] == '0':
					e.append(b)
			k[j] = e.copy()
	else:
		c += '1'
print(int(c, 2))


import math
import os
import random
import re
import sys
from collections import defaultdict, Counter, deque
import heapq
from functools import lru_cache
from itertools import accumulate, chain, combinations, permutations, groupby, zip_longest
from math import floor, ceil, sqrt

def solve(A, B):
	res = 0
	for i in range(9, -1, -1):
		for a in A:
			mn = 2 ** 10
			for b in B:
				a >> i
				b >> i
				mn = min(mn, res | a >> i & b >> i)
			res = mn
		res = mn << 1
	res = res >> 1
	return res
(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(solve(A, B))


(n, m) = map(int, input().split())
l = [int(j) for j in input().split()]
l1 = [int(j) for j in input().split()]
c = 0
for t in range(2 ** 9 + 1):
	c = 1
	for i in range(len(l)):
		a = 0
		for j in range(len(l1)):
			if t | l[i] & l1[j] == t:
				a = 1
				break
		if a == 0:
			c = 0
			break
	if c == 1:
		print(t)
		break


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def solve():
	(n, m) = aj()
	A = aj()
	B = aj()
	for k in range(600):
		c = 0
		for i in A:
			f = 0
			for j in B:
				if k | i & j == k:
					f = 1
					break
			if f:
				c += 1
		if c == n:
			print(k)
			exit(0)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
solve()


import sys
from collections import defaultdict as dd
from collections import deque
from functools import *
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def inc(d, c):
	d[c] = d[c] + 1 if c in d else 1

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j):
	return 0 <= i < n and 0 <= j < n

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = 1
while t > 0:
	t -= 1
	(n, m) = mi()
	a = li()
	b = li()
	k = 0
	d = [[0 for i in range(2 ** 9 + 5)] for j in range(n + 1)]
	for i in range(n):
		if i == 0:
			for j in range(m):
				d[i][a[i] & b[j]] = 1
		else:
			for j in range(m):
				for k in range(2 ** 9 + 1):
					if d[i - 1][k] == 1:
						d[i][k | a[i] & b[j]] = 1
	print(d[n - 1].index(1))


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
from fractions import Fraction
INF = float('inf')
mod = int(1000000000.0) + 7
(n, m) = mdata()
a = mdata()
b = mdata()
l = []
for i in range(n):
	l.append([a[i] & b[j] for j in range(m)])
set1 = set(l[0])
for i in range(1, n):
	set2 = set()
	for j in set1:
		for k in range(m):
			set2.add(j | l[i][k])
	set1 = set2
out(min(set1))


import sys
input = sys.stdin.readline

def solve():
	(n, m) = map(int, input().split())
	a = [*map(int, input().split())]
	b = [*map(int, input().split())]
	ans = int()
	for x in range(pow(2, 9) + 1):
		flag = True
		for i in a:
			flag &= any((i & j | x == x for j in b))
		if flag:
			print(x)
			exit()
solve()


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
for i in range(513):
	c = 0
	for j in arr:
		for k in arr2:
			if j & k | i == i:
				c += 1
				break
	if c == n:
		print(i)
		break


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = set(map(int, input().split()))
max_c = 2 ** 9

def checkNum(num, c, bin_c):
	if num > c:
		return False
	bin_num = bin(num)
	for i in range(0, len(bin_num) - 2):
		if bin_num[len(bin_num) - 1 - i] == '1' and bin_c[len(bin_c) - 1 - i] == '0':
			return False
	return True
for c in range(0, max_c + 1):
	bin_c = bin(c)
	good = True
	for ai in a:
		found = False
		for bi in b:
			if checkNum(bi & ai, c, bin_c):
				found = True
				break
		if not found:
			good = False
			break
	if good:
		print(c)
		exit()


def fun(lis, num, n, m):
	for i in range(n):
		ans = 0
		for j in range(m):
			ans |= lis[i][j] | num == num
		if ans == 0:
			return 0
	return 1
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
lis = [[0] * m for i in range(n)]
for i in range(n):
	for j in range(m):
		lis[i][j] = a[i] & b[j]
for i in range(512):
	if fun(lis, i, n, m):
		print(i)
		break


def bitwise(n, a1, m, a2):
	for i in range(0, 2 ** 9 + 1):
		for j in range(n):
			c = True
			for k in range(m):
				if a1[j] & a2[k] | i == i:
					break
				elif k == m - 1:
					c = False
			if c == False:
				break
		if c:
			return i
(n, m) = list(map(int, input().split(' ')))
a1 = list(map(int, input().split(' ')))
a2 = list(map(int, input().split(' ')))
print(bitwise(n, a1, m, a2))


(n, m) = map(int, input().split())
nn = list(map(int, input().split()))
mm = list(map(int, input().split()))
l = []
for i in nn:
	x = sorted([j & i for j in mm])
	if 0 not in x:
		l.append(x)
ll = [0]
for ii in l:
	lll = []
	for i in ii:
		for j in ll:
			lll.append(i | j)
	lll = list(set(lll))
	ll = lll
print(min(ll))


from math import floor, log2

def find_min_or(a_seq, b_seq):
	bits_n = max(map(_get_number_bitlength, a_seq))
	success_bits = []
	selected_b = [None] * len(a_seq)
	new_selected_b = selected_b.copy()
	for bit_index in reversed(range(bits_n)):
		for (i_a, a) in enumerate(a_seq):
			for b in b_seq:
				if _get_bit_by_index(a & b, bit_index) == 0:
					if all((_get_bit_by_index(a & b, bit_index) == 0 for bit_index in success_bits)):
						new_selected_b[i_a] = b
		if None not in new_selected_b and _get_bit_by_index(_compute_or(a_seq, new_selected_b, bits_n), bit_index) == 0:
			selected_b = new_selected_b
			success_bits.append(bit_index)
		new_selected_b = selected_b.copy()
	if not success_bits:
		return _compute_or(a_seq, [b_seq[0]] * len(a_seq), bits_n)
	return _compute_or(a_seq, selected_b, bits_n)

def _compute_or(a_seq, selected_b, bits_n):
	min_value = 0
	for (a, b) in zip(a_seq, selected_b):
		min_value |= a & b
	return min_value

def _get_number_bitlength(num):
	result = floor(log2(num)) + 1 if num != 0 else 1
	return result

def _get_bit_by_index(num, i):
	return num >> i & 1

def main():
	(n, m) = map(int, input().split())
	a_seq = tuple(map(int, input().split()))
	b_seq = tuple(map(int, input().split()))
	min_value = find_min_or(a_seq, b_seq)
	print(min_value)
main()


from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
rlf = lambda : map(float, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))
MOD = 10 ** 9 + 7

def main():
	(n, m) = rli()
	A = list(rli())
	B = list(rli())
	ans = INF
	for target in range(1 << 10):
		target_good = True
		for a in A:
			a_good = False
			for b in B:
				if a & b | target == target:
					a_good = True
					break
			if not a_good:
				target_good = False
				break
		if target_good:
			print(target)
			break
	stdout.close()
main()


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
possible = [[] for i in range(n)]
for i in range(n):
	for j in range(m):
		possible[i].append(a[i] & b[j])
import sys
for answer in range(2 ** 9):
	answer_flag = 1
	for i in range(n):
		flag = 0
		for x in possible[i]:
			if answer | x == answer:
				flag = 1
				break
		if flag == 0:
			answer_flag = 0
			break
	if answer_flag:
		print(answer)
		return


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for target in range(2 ** 9 + 1):
	f = 0
	for i in a:
		fg = 0
		for j in b:
			if i & j | target == target:
				fg = 1
				continue
		if fg == 0:
			break
		else:
			f += 1
	if f == n:
		print(target)
		break


(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, dgraphs, mod) = (lambda : sorted(zz()), {}, 10 ** 9 + 7)
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
(n, m) = zz()
a = zz()
b = zz()
for i in range(2 ** 9):
	for j in range(n):
		for k in range(m):
			if i | a[j] & b[k] == i:
				break
		else:
			break
	else:
		print(i)
		exit()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(513):
	x = 0
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				x += 1
				break
	if x == n:
		print(i)
		exit()


def get_not_in(local_arr, bit):
	wrongs = []
	for i in range(len(local_arr)):
		if local_arr[i][bit] == '1':
			wrongs.append(i)
	return wrongs
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
arr = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	for j in range(m):
		x = bin(a[i] & b[j])[2:]
		arr[i][j] = '0' * (10 - len(x)) + x
ans = [0] * 10
for i in range(10):
	all_wrongs = []
	for j in arr:
		wrong_numbs = get_not_in(j, i)
		all_wrongs.append(wrong_numbs)
	flag = True
	pos = 0
	for j in all_wrongs:
		if len(j) == len(arr[pos]):
			ans[i] = 1
			flag = False
		pos += 1
	if flag:
		for j in range(len(all_wrongs)):
			for k in range(len(all_wrongs[j])):
				del arr[j][all_wrongs[j][k] - k]
ans_numb = 0
for i in range(len(ans)):
	ans_numb += 2 ** (9 - i) * ans[i]
print(ans_numb)


(n, m) = map(int, input().split())
(*a_arr,) = map(int, input().split())
(*b_arr,) = map(int, input().split())
dp = [[0] * (1 << 9) for _ in range(n + 1)]
dp[0][0] = True
for i in range(n):
	for mask in range(1 << 9):
		for j in range(m):
			if dp[i][mask]:
				res_mask = mask | a_arr[i] & b_arr[j]
				dp[i + 1][mask | a_arr[i] & b_arr[j]] = True
print(min((mask for mask in range(1 << 9) if dp[n][mask])))


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
ans = -1
for mask in range(2 ** 9):
	bol = True
	for i in arr:
		b = False
		for j in brr:
			if ~mask & (i & j) == 0:
				b = True
				break
		if not b:
			bol = False
	if bol:
		ans = mask
		break
print(ans)


import sys
input = sys.stdin.buffer.readline
I = lambda : list(map(int, input().split()))
(n, m) = I()
a = I()
b = I()
p = [a[0] & b[j] for j in range(m)]
for kk in range(513):
	an = kk
	ct = 0
	for i in range(n):
		po = 0
		for j in range(m):
			if a[i] & b[j] | kk == kk:
				po = 1
				break
		if po:
			ct += 1
		else:
			break
	if ct == n:
		print(kk)
		sys.exit()


(n, m) = input().split()
s = list(map(int, input().split()))
l = list(map(int, input().split()))
for i in range(0, 2 ** 9 + 1):
	for j in range(len(s)):
		c = True
		for k in range(len(l)):
			if s[j] & l[k] | i == i:
				break
			elif k == len(l) - 1:
				c = False
		if c == False:
			break
	if c:
		print(i)
		break


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def matrix(n):
	return [list(map(int, input().split())) for i in range(n)]

def count2Dmatrix(i, list):
	return sum((c.count(i) for c in list))

def modinv(n, p):
	return pow(n, p - 2, p)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
(n, m) = spin()
a = lin()
b = lin()
for ans in range(512):
	poss = True
	for i in a:
		curr = False
		for j in b:
			if i & j | ans == ans:
				curr = True
		if curr == False:
			poss = False
	if poss == True:
		print(ans)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
flag1 = False
for i in range(513):
	flag1 = True
	for j in a:
		flag2 = False
		for k in b:
			if i | j & k == i:
				flag2 = True
				break
		if flag2 == False:
			flag1 = False
			break
	if flag1:
		print(i)
		break


f = input
f()
a = [*map(int, f().split())]
b = [*map(int, f().split())]
for x in range(512):
	r = 1
	for v in a:
		r &= any((v & j | x == x for j in b))
	if r:
		print(x)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(2 ** 9):
	flag = 1
	for j in range(n):
		f = 0
		for k in range(m):
			if a[j] & b[k] | i == i:
				f = 1
				break
		if f == 0:
			flag = 0
			break
	if flag:
		print(i)
		break


(n, m) = map(int, input().split())
alist = list(map(int, input().split()))
blist = list(map(int, input().split()))
ma = max(alist)
num = 0
while 1 << num < ma:
	num += 1
f = {}
for i in range(0, n):
	f[i] = blist
res = 0
while num >= 0:
	flag = True
	tmp = 1 << num
	for i in range(0, n):
		if alist[i] & tmp == 0:
			continue
		if len([j for j in f[i] if j & tmp == 0]) == 0:
			flag = False
			break
	if not flag:
		res += 1 << num
	else:
		for i in range(0, n):
			if alist[i] & tmp == 0:
				continue
			f[i] = [j for j in f[i] if j & tmp == 0]
	num -= 1
print(res)


import sys
from math import ceil, factorial, gcd
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
MOD = 10 ** 9 + 7
INF = float('inf')
rl = lambda : list(map(int, sys.stdin.readline().split()))
rs = lambda : sys.stdin.readline().strip()
(n, m) = rl()
A = rl()
B = rl()
dp1 = set([0])
for a in A:
	dp2 = set()
	for b in B:
		for d in dp1:
			dp2.add(d | a & b)
	dp1 = dp2
print(min(dp1))


(n, m) = map(int, input().split())
x = 0
q = list(map(int, input().split()))
w = list(map(int, input().split()))
s = [[0 for j in range(m)] for i in range(n)]
for i in range(n):
	for j in range(m):
		s[i][j] = q[i] & w[j]
t = 2 ** 9
used = [[0 for j in range(m)] for i in range(n)]
while t > 0:
	c = 0
	for i in range(n):
		p = 1
		o = 0
		for j in range(m):
			if used[i][j] == 0:
				p = min(p, s[i][j] // t)
				o = 1
		if o == 1:
			c = max(c, p)
	if c == 1:
		x += t
	else:
		for i in range(n):
			for j in range(m):
				if s[i][j] >= t:
					used[i][j] = 1
	for i in range(n):
		for j in range(m):
			s[i][j] %= t
	t //= 2
print(x)


(n, x) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
for i in range(513):
	flag1 = 0
	for j in A:
		flag2 = 0
		for k in B:
			if j & k | i == i:
				flag2 = 1
		if flag2 != 1:
			break
	else:
		ans = i
		break
print(i)


from sys import stdin
inp = lambda : stdin.readline().strip()
(n, m) = [int(x) for x in inp().split()]
a = [int(x) for x in inp().split()]
b = [int(x) for x in inp().split()]
ans = float('inf')
for k in range(len(a)):
	currAns = 0
	for i in a[k:]:
		curr = i & b[0]
		for j in b[1:]:
			if currAns | curr > currAns | i & j:
				curr = i & j
		currAns |= curr
	for i in a[:k]:
		curr = i & b[0]
		for j in b[1:]:
			if currAns | curr > currAns | i & j:
				curr = i & j
		currAns |= curr
	ans = min(ans, currAns)
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for ans in range(2 ** 9):
	for i in range(n):
		ok = False
		for g in range(m):
			if ans == a[i] & b[g] | ans:
				ok = True
				break
		if not ok:
			break
	if ok:
		print(ans)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(2000000001):
	bool1 = True
	for j in a:
		bool2 = False
		for k in b:
			if j & k | i == i:
				bool2 = True
		if not bool2:
			bool1 = False
			break
	if bool1:
		print(i)
		break


def check(s1, s2):
	temp = True
	for i in range(0, len(s1)):
		if s1[i] == '1' and s2[i] == '0':
			temp = False
			break
	return temp
(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
BN = []
for i in range(m):
	tt = bin(b[i])[2:]
	BN.append(str(tt))
st = ''
for ele in range(0, 9):
	flag = '0'
	for i in range(0, n):
		temp = 0
		for j in range(m):
			c = a[i] & b[j]
			tt = bin(c)[2:]
			tt = '0' * (9 - len(tt)) + tt
			if check(tt[:len(st)], st) and tt[len(st)] == '0':
				temp = 1
				break
		if temp == 0:
			flag = '1'
			break
	st += flag
fnl = int(st, 2)
print(fnl)


(n, m) = [int(i) for i in input().split(' ')]
a = [int(i) for i in input().split(' ')]
b = [int(i) for i in input().split(' ')]
ab = [b.copy() for i in a]
result = 1023

def isTrue(a, s):
	return a >> s & 1
for s in range(9, -1, -1):
	new_ab = [None for i in ab]
	possible = True
	for (i, ca) in enumerate(a):
		if isTrue(ca, s):
			res = [cb for cb in ab[i] if not isTrue(cb, s)]
			if len(res) != 0:
				new_ab[i] = res
			else:
				possible = False
				break
		else:
			new_ab[i] = ab[i]
	if possible:
		result ^= 1 << s
		ab = new_ab
print(result)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(512):
	fl1 = True
	for ai in a:
		fl = True
		for bi in b:
			if ai & bi | i == i:
				fl = False
				break
		if fl:
			fl1 = False
			break
	if fl1:
		print(i)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for ans in range(2000000001):
	cnt = 0
	for i in range(n):
		for j in range(m):
			c = a[i] & b[j]
			A = c | ans
			if A == ans:
				cnt += 1
				break
	if cnt == n:
		print(ans)
		break


import math
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = 0
ok = [[0 for x in range(m + 1)] for x in range(n + 1)]
for k in range(8, -1, -1):
	zero = True
	for i in range(n):
		isZero = False
		for j in range(m):
			if not ok[i][j] and (not a[i] & b[j] & 1 << k):
				isZero = True
		zero &= isZero
	if not zero:
		res += 1 << k
		continue
	for i in range(n):
		for j in range(m):
			ok[i][j] = ok[i][j] | (a[i] & b[j] & 1 << k != 0)
print(res)


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
f = 0
for k in range(1 << 9 + 1):
	count = 0
	for i in a:
		for j in b:
			if i & j | k == k:
				count += 1
				break
		if count == n:
			print(k)
			f = 1
			break
	if f:
		break


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cur = (1 << 10) - 1
bit = []
for i in range(9, -1, -1):
	for el1 in a:
		isThere = False
		for el2 in b:
			el = el1 & el2
			if el <= cur and el & 1 << i == 0 and (cur | el == cur):
				isThere = True
				break
		if not isThere:
			break
	if isThere:
		cur = cur - (1 << i)
print(cur)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
for x in range(2 ** 9):
	count = 0
	for i in a:
		for j in b:
			if i & j | x == x:
				count += 1
				break
		if count == n:
			break
	if count == n:
		print(x)
		break


from math import *
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(513):
	flag2 = 1
	for j in range(n):
		flag1 = 0
		for k in range(m):
			if i | a[j] & b[k] == i:
				flag1 = 1
				break
		if flag1 == 1:
			continue
		else:
			flag2 = 0
			break
	if flag2 == 1:
		break
print(i)


import sys
input = sys.stdin.buffer.readline

def solution():
	(n, m) = map(int, input().split())
	ln = list(map(int, input().split()))
	lm = list(map(int, input().split()))
	for s in range(512):
		cnt = 0
		for i in range(n):
			for j in range(m):
				if ln[i] & lm[j] | s == s:
					cnt += 1
					break
		if cnt == n:
			break
	print(s)
t = 1
for _ in range(t):
	solution()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
available = {}
for x in a:
	available[x] = b
for i in reversed(range(0, 10)):
	bit = 2 ** i
	can_do = True
	for x in a:
		if not x & bit:
			continue
		cur_can_do = False
		for y in available[x]:
			if not y & bit:
				cur_can_do = True
				break
		if not cur_can_do:
			can_do = False
			break
	if can_do:
		for x in a:
			if not x & bit:
				continue
			available[x] = list(filter(lambda y: not y & bit, available[x]))
c = 0
for x in a:
	c |= x & list(available[x])[0]
print(c)


def works(ans, a, b):
	val = 0
	for i in range(len(a)):
		flag = 0
		for j in range(len(b)):
			x = a[i] & b[j]
			if x & ~ans == 0:
				flag = 1
				break
		if flag:
			val += 1
	if val == len(a):
		return 1
	return 0
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(2 ** 9 + 2):
	if works(i, a, b):
		print(i)
		break


def check(mat, x):
	n = len(mat)
	m = len(mat[0])
	for i in range(n):
		t = 0
		for j in range(m):
			t |= mat[i][j] | x == x
		if t == 0:
			return 0
	return 1
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
mat = [[0] * m for i in range(n)]
for i in range(n):
	for j in range(m):
		mat[i][j] = a[i] & b[j]
for i in range(512):
	if check(mat, i):
		print(i)
		break


from sys import stdin
input = stdin.readline
(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
for x in range(512):
	flag = 0
	r = '0' * 9 + bin(x)[2:]
	for y in a:
		flag = 0
		for z in b:
			s = '0' * 9 + bin(y & z)[2:]
			flag = 0
			for i in range(-9, 0, 1):
				if s[i] == '1' and r[i] == '0':
					flag = 1
					break
			if flag == 0:
				break
		if flag == 0:
			continue
		else:
			break
	if flag == 0:
		print(x)
		break


import sys
(n, m) = map(int, input().split(' '))
arrn = list(map(int, input().split(' ')))
arrm = list(map(int, input().split(' ')))
for k in range(512):
	found = True
	for i in range(n):
		flag = False
		for j in range(m):
			if arrn[i] & arrm[j] | k == k:
				flag = True
				break
		if not flag:
			found = False
			break
	if found:
		print(k)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0
for i in range(512):
	f = True
	for j in range(n):
		g = False
		for k in range(m):
			if a[j] & b[k] | i == i:
				g = True
				break
		f &= g
	if f:
		ans = i
		break
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
usable_b = []
for i in range(n):
	usable_b.append(range(m))
ans = 0
for d in range(9, -1, -1):
	new_usable_b = []
	can0 = True
	for i in range(n):
		usable = []
		for j in usable_b[i]:
			c = a[i] & b[j]
			if c & 1 << d == 0:
				usable.append(j)
		new_usable_b.append(usable)
		if len(usable) == 0:
			can0 = False
	if can0:
		usable_b = new_usable_b
	else:
		ans |= 1 << d
print(ans)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
l = 2 ** 9
D0 = [0] * l
for i in range(m):
	D0[A[0] & B[i]] = 1
for i in range(1, n):
	D1 = [0] * l
	C = []
	for j in range(m):
		C.append(A[i] & B[j])
	for j in range(len(C)):
		for k in range(l):
			if D0[k] == 1:
				D1[k | C[j]] = 1
	D0 = D1[:]
for i in range(l):
	if D0[i] == 1:
		print(i)
		break


def ss(a, b):
	n = len(a)
	m = len(b)

	def check(x):
		for i in a:
			t = 0
			for j in b:
				if i & j | x == x:
					t = 1
					break
			if t == 0:
				return False
		return True
	for i in range(1025):
		if check(i):
			return i
	return -1
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(ss(a, b))


s = input()
s = input()
s1 = s.split()
a = [int(i) for i in s1]
s = input()
s1 = s.split()
b = [int(i) for i in s1]
for i in range(0, 2 ** 9 + 1):
	for j in range(len(a)):
		flag = 0
		for k in range(len(b)):
			if a[j] & b[k] | i == i:
				flag = 1
		if flag == 0:
			break
	if flag == 1:
		print(i)
		break


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
st = [set()] * 205
for i in range(n):
	st[i] = set(brr)
ans = 0
for bit_pos in range(31, -1, -1):
	ans = ans | 1 << bit_pos
	flag2 = 1
	for i in range(n):
		flag = 0
		for item in st[i]:
			tmp = arr[i] & item
			if tmp & 1 << bit_pos == 0:
				flag = 1
				break
		if flag == 0:
			flag2 = 0
			break
	if flag2 == 0:
		continue
	for i in range(n):
		sttmp = set()
		for item in st[i]:
			tmp = arr[i] & item
			if tmp & 1 << bit_pos == 0:
				sttmp.add(item)
		st[i] = sttmp
	ans = ans & ~(1 << bit_pos)
print(ans)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
(N, M) = MAP()
A = LIST()
B = LIST()
ans = INF
for bit in range(1 << 9):
	good = 1
	for a in A:
		ok = 0
		for b in B:
			if bit | a & b == bit:
				ok = 1
				break
		if not ok:
			good = 0
			break
	if good:
		ans = min(ans, bit)
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	for ans in range(0, 1024):
		f = True
		for i in range(n):
			found = False
			for j in range(m):
				if a[i] & b[j] | ans == ans:
					found = True
					break
			if not found:
				f = False
				break
		if f:
			print(ans)
			break


import os
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline

def lii():
	return list(map(int, input().split()))

def ii():
	return int(input())
(n, m) = lii()
A = lii()
B = lii()
for res in range(0, 2 ** 9):
	flag = True
	for a in A:
		found = False
		for b in B:
			c = a & b
			if c | res == res:
				found = True
				break
		if not found:
			flag = False
			break
	if flag:
		print(res)
		break
	else:
		continue


def gt():
	return list(map(int, input().split()))
(n, m) = gt()
a = gt()
b = gt()

def gtmx():
	return max([min([(a[i] & b[j]).bit_length() for j in range(m)]) for i in range(n)])
ret = 0
while 1:
	high = gtmx()
	if high == 0:
		break
	val = 1 << high - 1
	ret |= val
	for i in range(n):
		if a[i] & val > 0:
			a[i] ^= val
print(ret)


import sys
buff_readline = sys.stdin.readline
readline = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(buff_readline())

def read_int_n():
	return list(map(int, buff_readline().split()))

def read_float():
	return float(buff_readline())

def read_float_n():
	return list(map(float, buff_readline().split()))

def read_str():
	return readline().strip()

def read_str_n():
	return readline().strip().split()

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

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, M, A, B):
	cb = [B[:] for _ in range(N)]
	ans = 0
	for i in range(64, -1, -1):
		ncb = []
		m = (1 << i + 1) - 1
		d = 1 << i
		for (j, a) in enumerate(A):
			t = []
			for b in cb[j]:
				if a & b & m < d:
					t.append(b)
			if len(t) == 0:
				break
			else:
				ncb.append(t)
		if len(ncb) == N:
			cb = ncb
		else:
			ans |= 1 << i
	return ans

def main():
	(N, M) = read_int_n()
	A = read_int_n()
	B = read_int_n()
	print(slv(N, M, A, B))
main()


import sys, math
input = sys.stdin.readline
(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
ans = 999999999999999999999
for k in range(513):
	flag = True
	for i in range(n):
		tempFlag = False
		for j in range(m):
			temp = l1[i] & l2[j]
			if temp | k == k:
				tempFlag = True
				break
		if tempFlag == False:
			flag = False
			break
	if flag:
		ans = min(ans, k)
print(ans)


(n, m) = map(int, input().split())
a = [int(v) for v in input().split()]
b = [int(v) for v in input().split()]
j = 0
while j < 512:
	k = 0
	while k < n:
		f = 0
		for v in range(m):
			if a[k] & b[v] | j == j:
				f = 1
		if f == 0:
			j = j + 1
			break
		else:
			k = k + 1
	else:
		print(j)
		break


import sys
read = lambda : sys.stdin.readline().rstrip('\r\n')
(m, n) = map(int, read().split())
arr_a = list(map(int, read().split()))
arr_b = list(map(int, read().split()))

def solution(arr_a, arr_b):
	mask = 0
	while mask <= 2 ** 9:
		for a in arr_a:
			for b in arr_b:
				if ~mask & (a & b) == 0:
					break
			else:
				break
		else:
			return mask
		mask += 1
print(solution(arr_a, arr_b))


def main():
	(n, m) = map(int, input().split())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	dp = [[False for j in range(2 ** 9)] for i in range(n + 1)]
	dp[0][0] = True
	for i in range(n):
		for j in range(pow(2, 9)):
			if dp[i][j]:
				for k in arr2:
					dp[i + 1][j | arr1[i] & k] = True
	for i in range(2 ** 9):
		if dp[n][i]:
			print(i)
			return
main()


def format_bin(b):
	z = 12 - len(b)
	return '0' * z + b[2:]

def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = 0
	T = []
	M = 0
	for i in a:
		T.append([])
		for j in b:
			r = format_bin(bin(i & j))
			T[-1].append(r)
			M = max(M, len(r))
	c = ''
	cut = False
	for k in range(M):
		k_done = False
		for i in range(n):
			necessary = True
			for j in range(m):
				cell = T[i][j]
				if not cell:
					continue
				if cut and cell[k - 1] == '1':
					T[i][j] = None
					continue
				if not k_done and necessary:
					if cell[k] == '0':
						necessary = False
						if not cut:
							break
			if necessary:
				k_done = True
				if not cut:
					break
		if k_done:
			c += '1'
			cut = False
		else:
			c += '0'
			cut = True
	print(int(c, 2))
main()


(n, m) = map(int, input().split())
a = set(map(int, input().split()))
b = set(map(int, input().split()))
z = 1 << 7
R = {x: b.copy() for x in a}
for i in range(8, -1, -1):
	t = {x: set() for x in a}
	flag = True
	for p in a:
		for q in R[p]:
			if not p & q & 1 << i:
				t[p].add(q)
		if not t[p]:
			flag = False
	if flag:
		R = t
c = 0
z = 0
for x in a:
	for i in R[x]:
		z = i
	c |= x & z
print(c)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, M, A, B):
	for target in range(1 << 9):
		allGood = True
		for a in A:
			good = False
			for b in B:
				ab = a & b
				if ab & target == ab:
					good = True
					break
			if not good:
				allGood = False
				break
		if allGood:
			return target
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(N, M) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
ans = solve(N, M, A, B)
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(1 << 9):
	for j in a:
		for k in b:
			if k & j | i == i:
				break
		else:
			break
	else:
		print(i)
		break


def check(A):
	for j in range(n):
		t = 0
		for k in range(m):
			t = a[j] & b[k] | A == A
			if t:
				break
		if not t:
			return 0
	return 1
(n, m) = map(int, input().split())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
for i in range(2 ** 9):
	if check(i):
		print(i)
		exit(0)


(n, m) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in a:
	c.append([i & j for j in b])
(left, right) = (-1, 512)
while right - left > 1:
	middle = (left + right) // 2
	err = False
	for i in c:
		err = True
		for j in i:
			if j | middle == middle:
				err = False
				break
		if err:
			break
	if err:
		left = middle
	else:
		right = middle
print(left + 1)


from math import *
from collections import *
from random import *
from decimal import Decimal
from heapq import *
from bisect import *
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 5)

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())

def inp():
	return int(input())

def st():
	return input().rstrip('\n')
t = 1
while t:
	t -= 1
	(n, m) = ma()
	a = lis()
	b = lis()
	brute = []
	x = 0
	for i in range(n):
		ca = []
		for j in range(m):
			z = b[j] & a[i]
			z = bin(z)[2:]
			z = z.zfill(10)
			ca.append(z)
		brute.append(ca)
	fl = 0
	for i in range(0, 2 ** 9):
		z = bin(i)[2:]
		z = z.zfill(10)
		co = 0
		for j in brute:
			bitflag = 0
			for k in j:
				loc = 0
				for k1 in range(10):
					if z[k1] == k[k1]:
						continue
					if z[k1] == '1' and k[k1] == '0':
						continue
					else:
						loc = 1
						break
				if loc == 0:
					bitflag = 1
					co += 1
					break
			if bitflag == 0:
				break
			else:
				continue
		if co == n:
			print(i)
			exit(0)


(N, M) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for check in range(0, 2 ** 9 + 1):
	count = 0
	for i in range(len(A)):
		temp = 0
		for j in range(len(B)):
			if A[i] & B[j] | check == check:
				temp = 1
				break
		if temp == 1:
			count += 1
	if count == len(A):
		print(check)
		break


import sys
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : sys.stdin.readline().strip().split()
ri = lambda : int(sys.stdin.readline().strip())
rs = lambda : sys.stdin.readline().strip()
from collections import defaultdict as df
import random
from queue import PriorityQueue as pq
from math import ceil
(n, m) = RI()
a = RI()
b = RI()
mask = (1 << 12) - 1
for i in range(520):
	f = 0
	ans = 0
	for j in range(n):
		f1 = 0
		for k in range(m):
			temp = a[j] & b[k]
			invert = i ^ mask
			if temp & invert == 0:
				f1 = 1
				break
		if f1 == 0:
			f = 1
			break
	if f == 0:
		print(i)
		break


(n, m) = map(int, input().split())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
for ans in range(2 ** 9):
	for (i, aa) in enumerate(a):
		for (j, bb) in enumerate(b):
			if ans | aa & bb == ans:
				break
		else:
			break
	else:
		print(ans)
		break


(n, m) = map(int, input().split())
ans = 0
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
se = set()
i = arr1[0]
for j in arr2:
	se.add(i & j)
for i in arr1[1:]:
	p = set()
	for j in arr2:
		p.add(i & j)
	m = set()
	for k in p:
		m |= {k | j for j in se}
	se = m
print(min(se))


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(arr, brr):
	console('----- solving ------')
	for candidate in range(512):
		for a in arr:
			for b in brr:
				if a & b | candidate == candidate:
					break
			else:
				break
		else:
			return candidate

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
_ = list(map(int, input().split()))
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
res = solve(arr, brr)
print(res)


import sys, threading
sys.setrecursionlimit(10 ** 8)
threading.stack_size(10 ** 8)

def isValid(numa, numb, targetNum):
	mask = 1
	for i in range(9):
		if not targetNum & mask and numa & mask and numb & mask:
			return False
		mask <<= 1
	return True

def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for targetNum in range(2 ** 9):
		c = []
		for numa in a:
			for numb in b:
				if isValid(numa, numb, targetNum):
					c.append(numa & numb)
					break
			else:
				break
		if len(c) == len(a):
			out = 0
			for num in c:
				out |= num
			print(out)
			break
threading.Thread(target=main).start()


a = input().split(' ')
d = input().split(' ')
e = input().split(' ')
ans = 10000000000
p = 0
list1 = []
for i in d:
	for j in e:
		list1.append(int(i) & int(j))
list1 = list(set(list1))
list1.sort()
for l in list1:
	dict = {l: 0}
	p = l
	for i in range(len(d)):
		tag1 = 1000000000
		q = 0
		for j in e:
			tag = int(d[i]) & int(j)
			for x in dict:
				if tag & x == tag:
					q = 1
					tag1 = tag
					break
			if q == 1:
				break
			else:
				tag1 = min(tag, tag1)
		p = p | tag1
		dict[tag1] = 0
	ans = min(p, ans)
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
min_val = 0
not_found = True
while not_found is True:
	for i in range(n):
		found = False
		for j in range(m):
			if a[i] & b[j] | min_val == min_val:
				found = True
				break
		if found is False:
			min_val += 1
			break
		if i == n - 1 and found is True:
			not_found = False
print(min_val)


def is_valid(mask):
	for i in range(n):
		flag = False
		for j in range(m):
			ans = a[i] & b[j]
			if ans | mask == mask:
				flag = True
				break
		if not flag:
			return False
	return True
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(0, 1 << 9):
	if is_valid(i):
		print(i)
		break


from itertools import product
(n, m) = map(int, input().split())
ln = list(map(int, input().split()))
lm = list(map(int, input().split()))
ls = [[u & v for v in lm] for u in ln]
used = [[1] * m for _ in range(n)]
res = 0
for b in range(8, -1, -1):
	zero = 1
	for u in range(n):
		ok = any((used[u][v] and (not ls[u][v] & 1 << b) for v in range(m)))
		if not ok:
			zero = 0
			break
	if zero:
		for (u, v) in product(range(n), range(m)):
			if used[u][v] and ls[u][v] & 1 << b:
				used[u][v] = 0
	res += 0 if zero else 1 << b
print(res)


(n, m) = [int(x) for x in input().split(' ')]
al = [int(x) for x in input().split(' ')]
bl = [int(x) for x in input().split(' ')]

def binn(x):
	y = bin(x)
	z = '0' * (11 - len(y)) + y[2:len(y)]
	zz = []
	for x in z:
		zz.append(int(x))
	return zz

def rev(x):
	q = 0
	for i in range(9):
		q += x[8 - i] * 2 ** i
	return q

def And(p, q):
	aaa = [-1] * 9
	for i in range(9):
		aaa[i] = p[i] & q[i]
	return aaa

def Or(p, q):
	aaa = [-1] * 9
	for i in range(9):
		aaa[i] = p[i] | q[i]
	return aaa
a = []
for i in range(n):
	a.append(binn(al[i]))
b = []
for i in range(m):
	b.append(binn(bl[i]))
c = []
a.sort()
b.sort()
poss = []
for i in range(n):
	poss.append(set([x for x in range(0, m)]))
ans = [-1] * 9
for j in range(9):
	pd = []
	for x in poss:
		pd.append(set(x))
	for i in range(n):
		tbremoved = set()
		for x in pd[i]:
			if a[i][j] & b[x][j]:
				tbremoved.add(x)
		for x in tbremoved:
			pd[i].remove(x)
	fl = 1
	for i in range(n):
		if len(pd[i]) == 0:
			fl = 0
			break
	if fl:
		for i in range(len(poss)):
			poss[i] = set(pd[i])
		ans[j] = 0
	else:
		ans[j] = 1
print(rev(ans))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def solve(a, b, n, m):
	for d in range(2 ** 9):
		temp = 2 ** 9 - 1
		tfound = True
		for i in range(n):
			found = False
			for j in range(m):
				if a[i] & b[j] | d == d:
					found = True
					break
			if not found:
				tfound = False
				break
		if tfound:
			return d
	return 2 ** 9 - 1
print(solve(a, b, n, m))


(m, n) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort(reverse=True)
ans = []
arr = []
for i in range(0, 1000):
	temp = 0
	for j in range(0, m):
		flag = 0
		for k in range(0, n):
			if a[j] & b[k] | i == i:
				flag = 1
				break
		if flag == 0:
			break
		else:
			temp = temp + 1
	if temp == m:
		print(i)
		break


(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for_i_use_j = [[1] * m for _ in range(n)]
result = 0
for k in range(9, -1, -1):
	bit = 1 << k
	can_be_zero = 1
	for (i, a) in enumerate(A):
		can_be_zero_in_a = 1
		if a & bit != 0:
			flag = 0
			for j in range(m):
				if for_i_use_j[i][j] == 1 and B[j] & bit == 0:
					flag = 1
			can_be_zero_in_a = flag
		if can_be_zero_in_a == 0:
			can_be_zero = 0
	if can_be_zero == 1:
		for (i, a) in enumerate(A):
			if a & bit != 0:
				for j in range(m):
					if for_i_use_j[i][j] == 1 and B[j] & bit != 0:
						for_i_use_j[i][j] = 0
	else:
		result += bit
print(result)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 5
T = 1
for _ in range(T):
	(n, m) = list(map(int, stdin.readline().rstrip().split()))
	a = list(map(int, stdin.readline().rstrip().split()))
	b = list(map(int, stdin.readline().rstrip().split()))
	ans = inf
	for A in range(512):
		for i in range(n):
			isPossible = True
			for j in range(m):
				t = False
				if a[i] & b[j] | A == A:
					t = True
					break
			if not t:
				isPossible = False
				break
		if isPossible:
			ans = A
			break
	print(ans)


import sys
input = sys.stdin.buffer.readline

def main():
	(n, m) = map(int, input().split())
	alst = list(map(int, input().split()))
	blst = list(map(int, input().split()))
	clst = [[] for _ in range(n)]
	for (i, a) in enumerate(alst):
		for b in blst:
			clst[i].append(a & b)
	ans = set(clst[0])
	for lst in clst[1:]:
		ans2 = set()
		for num in ans:
			for num2 in lst:
				ans2.add(num | num2)
		ans = ans2.copy()
	print(min(ans))
main()


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	(n, m) = mi()
	a = sorted(li())
	b = sorted(li())
	ands = []
	for i in range(513):
		f = 1
		for j in a:
			for k in b:
				tmp = j & k | i
				if tmp == i:
					break
			else:
				f = 0
				break
		if f == 1:
			print(i)
			break
main()


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	(n, m) = sep()
	a = lis()
	b = lis()
	for ans in range(0, 2 ** 9 + 1):
		cnt = 0
		for i in range(n):
			for j in range(m):
				if a[i] & b[j] | ans == ans:
					cnt += 1
					break
		if cnt == n:
			print(ans)
			return
testcase(1)


from math import inf
from collections import Counter
(n, m) = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
cnt = Counter(arr2)
ans = 0
for num in range(512):
	f2 = 1
	for i in arr1:
		f = 1
		for j in arr2:
			if i & j | num == num:
				f = 0
		if f:
			f2 = 0
			break
	if f2 == 1:
		ans = num
		break
print(ans)


(n, m) = input().split()
n = int(n)
m = int(m)
a1 = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(513):
	for j in range(len(a1)):
		a = 0
		for k in range(len(b)):
			ans1 = a1[j] & b[k]
			ans = ans1 | i
			if ans == i:
				a = 1
				break
		if a == 1:
			continue
		else:
			break
	if a == 1:
		print(i)
		break


import sys
import math
import heapq
import collections
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')

def printspace(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer(' ')

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d

def check(x, p, d):
	for i in range(n):
		good = False
		for j in range(m):
			if p[i] & d[j] | x == x:
				good = True
				break
		if good:
			continue
		else:
			break
	if good:
		return True
	else:
		return False
(n, m) = inputnums()
a = inputlist()
b = inputlist()
ans = 0
for i in range(512):
	if check(i, a, b):
		ans = i
		break
print(ans)


from itertools import product

def main():
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	AB = []
	for a in A:
		q = [a & b for b in B]
		AB.append(q)
	for i in range(n):
		AB[i].sort()
	G = []
	for i in range(9):
		t = 2 ** i
		R = [[b & t == t for b in ab] for ab in AB]
		G.append(R)
	ans = 2 ** 10
	for P in product(range(2), repeat=9):
		check = True
		now = 0
		for i in range(n):
			for j in range(m):
				for k in range(9):
					if P[k] == 1:
						pass
					elif G[k][i][j]:
						break
				else:
					now |= AB[i][j]
					break
			else:
				check = False
		if check:
			if now < ans:
				ans = now
	print(ans)
main()


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
bl = len(bin(max(a + b))) - 2
ans = 0
gi = 0
for g in range(bl, -1, -1):
	gi |= 1 << g
	for i in [min([ai & bj & gi | ans for bj in b]) for ai in a]:
		ans |= i
print(ans)


(a, b) = map(int, input().split())
r1 = list(map(int, input().split()))
r2 = list(map(int, input().split()))
for target in range(2 ** 9):
	ans = 0
	g = True
	for i in range(a):
		f = False
		for j in range(b):
			t = r1[i] & r2[j]
			if t & target == t:
				f = True
				break
		if not f:
			g = False
			break
	if g:
		print(target)
		exit(0)


mem = [[-1 for j in range(201)] for i in range(1 << 10)]

def dp(i, j, n, m, a, b):
	if j == n:
		return 0
	if mem[i][j] != -1:
		return mem[i][j]
	ans = 1 << 10
	for k in range(m):
		ans = min(ans, i | a[j] & b[k] | dp(i | a[j] & b[k], j + 1, n, m, a, b))
	mem[i][j] = ans
	return ans
(n, m) = map(int, input().split())
a = [*map(int, input().split())]
ans = 0
b = [*map(int, input().split())]
print(dp(0, 0, n, m, a, b))


from collections import Counter
import string
import math
import sys

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
mod = 1000000007
(n, m) = vary(2)
a = array_int()
b = array_int()
ans = float('inf')
for k in range(0, 2 ** 9):
	for i in range(n):
		for j in range(m):
			if a[i] & b[j] | k == k:
				ans = k
				break
		else:
			break
	else:
		print(k)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for check in range(0, 2 ** 9 + 1):
	kitneAye = 0
	for i in range(n):
		flag = 0
		for j in range(m):
			if check | a[i] & b[j] == check:
				flag = 1
				break
		if flag == 0:
			break
		else:
			kitneAye += 1
	if kitneAye == n:
		print(check)
		break


def ok(num, x, y):
	for z in x:
		yep = False
		for d in y:
			cur = z & d
			if cur | num == num:
				yep = True
		if not yep:
			return False
	return True
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(513):
	if ok(i, a, b):
		print(i)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pref = [b for i in range(n)]
c = []
for q in range(9):
	k = 8 - q
	s = 0
	for i in range(n):
		if a[i] >> k & 1 == 1:
			t = pref[i][0] >> k & 1
			for v in pref[i]:
				t = t & (v >> k & 1)
			s = s | t
	if s == 0:
		for i in range(n):
			if a[i] >> k & 1 == 1:
				pref1 = []
				for v in pref[i]:
					if v >> k & 1 == 0:
						pref1.append(v)
				pref[i] = pref1
for i in range(n):
	c.append(a[i] & pref[i][0])
t = c[0]
for i in c:
	t = t | i
print(t)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
global c
c = 10 ** 10

@lru_cache(None)
def rec(i, val):
	global c
	if val > c:
		return
	if i == n:
		c = min(c, val)
		return
	for ele in B:
		rec(i + 1, val | A[i] & ele)
for _ in range(1):
	(n, m) = L()
	A = L()
	B = L()
	A.sort()
	B.sort()
	rec(0, 0)
	print(c)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
for i in range(512):
	m = 0
	for j in l1:
		f = 0
		for k in l2:
			if j & k | i == i:
				f = 1
				break
		if f == 0:
			m = 1
			break
	if m == 0:
		print(i)
		break


def isPossible(ans):
	for i in range(n):
		done = 0
		for j in range(m):
			if a[i] & b[j] | ans == ans:
				done = 1
				break
		if not done:
			return 0
	return 1
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
fin = 2 ** 9 - 1
for i in range(2 ** 9 - 1, -1, -1):
	if isPossible(i):
		fin = min(fin, i)
print(fin)


def solve():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for x in range(0, 2 ** 9):
		flag = True
		for i in range(len(a)):
			flag2 = False
			for j in range(len(b)):
				if a[i] & b[j] | x == x:
					flag2 = True
					break
			if not flag2:
				flag = False
				break
		if flag:
			print(x)
			return
solve()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = [[] for i in range(n)]
for i in range(n):
	d = dict()
	for j in range(m):
		if a[i] & b[j] not in d.keys():
			res[i].append(a[i] & b[j])
		d[a[i] & b[j]] = 1
ways = []
for j in res[0]:
	ways.append(j)
i = 1
while i < n:
	d = dict()
	newways = []
	arr = res[i]
	for j in arr:
		for p in ways:
			if j | p not in d.keys():
				newways.append(j | p)
			d[j | p] = 1
	ways = newways
	i += 1
print(min(ways))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(0, 2 ** 9):
	work = True
	for j in range(n):
		found = False
		for k in range(m):
			if i | a[j] & b[k] == i:
				found = True
				break
		if not found:
			work = False
			break
	if work:
		print(i)
		break


(x, y) = map(int, input().split())
s = list(map(int, input().split()))
z = list(map(int, input().split()))
for n in range(1 << 9 + 1):
	ct = 0
	for a in range(x):
		for b in range(y):
			p = n | s[a] & z[b]
			if p == n:
				ct += 1
				break
	if ct == x:
		print(n)
		break


def solve():
	ans = 0
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	for a in range(1 << 9):
		for i in range(N):
			for j in range(M):
				c = A[i] & B[j]
				if c & a == c:
					break
			else:
				break
		else:
			return a
print(solve())


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
from itertools import accumulate
from functools import lru_cache
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
(n, m) = li()
l1 = li()
l2 = li()
for i in range(2 ** 9 + 1):
	takecurrent = 1
	for j in l1:
		for k in l2:
			if i | j & k == i:
				takecurrent = 1
				break
			else:
				takecurrent = 0
		if not takecurrent:
			break
	if takecurrent:
		print(i)
		break


(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
for ans in range(512):
	for i in ar:
		for j in br:
			if ans | i & j == ans:
				bo = True
				break
		else:
			bo = False
			break
	if bo:
		print(ans)
		break


from collections import Counter
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')

def check(A):
	for i in range(n):
		fnd = False
		for j in range(m):
			if a[i] & b[j] | A == A:
				fnd = True
				break
		if not fnd:
			return False
	return True
(n, m) = gil()
a = gil()
b = gil()
for i in range(2 ** 9 + 1):
	if check(i):
		print(i)
		break


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mx = 2 ** 9
dp = [[0 for i in range(mx)] for i in range(n + 1)]
dp[0][0] = 1
for i in range(n):
	for j in range(mx):
		if dp[i][j]:
			for x in b:
				dp[i + 1][j | a[i] & x] = 1
for i in range(mx):
	if dp[n][i]:
		print(i)
		sys.exit(0)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
arr = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	for j in range(m):
		arr[i][j] = a[i] & b[j]
k = pow(2, 9)
for i in range(k):
	t = 0
	for j in range(n):
		f = 0
		for jj in range(m):
			if arr[j][jj] | i == i:
				f = 1
				break
		if f == 0:
			t = 1
			break
	if t == 0:
		print(i)
		break


import math
from decimal import *
import random

def tobit(n):
	if n == 0:
		return '0'
	else:
		ans = ''
		for i in range(int(math.log(n, 2)) + 1):
			if n >> i & 1:
				ans = '1' + ans
			else:
				ans = '0' + ans
		return ans

def same(a, b):
	for i in range(len(a)):
		if a[i] == b[i] == '1':
			return True
	return False
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = int(1000000000.0)
for x in range(2 ** 9 + 1):
	ok = False
	for i in range(n):
		for j in range(m):
			if a[i] & b[j] | x == x:
				ok = True
				break
			else:
				ok = False
		if not ok:
			break
	if ok:
		ans = int(x)
		break
print(ans)


(n, m) = map(int, input().split())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))
ll = []
for i in range(n):
	l = []
	for j in range(m):
		l.append(list1[i] & list2[j])
		l.sort()
	ll.append(l)
xx = 2 ** 10
for i in range(0, 2 ** 10):
	x = i
	ff = 0
	for j in range(n):
		f = 0
		for k in range(m):
			if x | ll[j][k] == x:
				f = 1
				break
		if f == 0:
			ff = 1
			break
	if ff == 0:
		if x < xx:
			xx = x
print(xx)


(n, m) = map(int, input().split())
ln = list(map(int, input().split()))
lm = list(map(int, input().split()))
for num in range(1 << 9):
	answer = 0
	for i in range(n):
		f = False
		for j in range(m):
			if ln[i] & lm[j] | num == num:
				f = True
		if f:
			answer += 1
			f = False
		else:
			continue
	if answer == n:
		print(num)
		break


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mv = []
f = 0
for c in range(512):
	s = 0
	for i in a:
		f = 0
		for j in b:
			v = i & j | c
			if v == c:
				f = 1
				s += 1
				break
		if f == 0:
			break
	if s == n:
		print(c)
		break


(n, m) = [int(i) for i in input().split()]
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
for target in range(0, 2 ** 10):
	for a in a_list:
		found = False
		for b in b_list:
			if a & b | target == target:
				found = True
				break
		if found == False:
			break
	if found == True:
		print(target)
		break


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve():
	(n, m) = IP()
	a = L()
	b = L()
	ans = 100000.0
	for aa in range(2 ** 9):
		cnt = 0
		for i in range(n):
			for j in range(m):
				if a[i] & b[j] | aa == aa:
					cnt += 1
					break
		if cnt == n:
			ans = min(ans, aa)
	P(ans)
	return
t = 1
for i in range(t):
	solve()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
S = dict()
for (i, x) in enumerate(a):
	S[i] = set()
	for y in b:
		S[i].add(x & y)
for i in range(2 ** 9 + 1):
	x = 0
	flag = False
	for j in range(n):
		flag = False
		for y in S[j]:
			if (x | y) ^ i | i == i:
				x = x | y
				flag = True
				break
		if flag:
			continue
		else:
			break
	if flag:
		print(i)
		break


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))

def isValid(x):
	for i in range(n):
		flag = False
		for j in range(m):
			res = arr[i] & brr[j]
			if res | x == x:
				flag = True
				break
		if flag == False:
			return False
	return True
for i in range(1 << 9):
	if isValid(i):
		print(i)
		break


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
(n, m) = MI()
aa = LI()
bb = LI()
atob = [bb[:] for _ in range(n)]
ans = 0
for k in range(8, -1, -1):
	nb = [[] for _ in range(n)]
	for (i, a) in enumerate(aa):
		ok = True
		for b in atob[i]:
			if (a & b) >> k & 1 == 0:
				nb[i].append(b)
		if not nb[i]:
			ans |= 1 << k
			break
	else:
		atob = nb
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
change = [[a[i] & b[j] for j in range(m)] for i in range(n)]
dp = [[0] * 2 ** 9 for i in range(n)]
for i in range(n):
	if i == 0:
		for k in range(m):
			dp[0][change[i][k]] = 1
		continue
	for j in range(2 ** 9):
		if dp[i - 1][j] == 1:
			for k in range(m):
				dp[i][j | change[i][k]] = 1
for j in range(2 ** 9):
	if dp[-1][j] == 1:
		print(j)
		break


import sys
input = sys.stdin.readline
from bisect import bisect_left
from bisect import bisect_right
from math import ceil, factorial

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
(n, m) = int_array()
a = int_array()
b = int_array()
for x in range(512):
	for i in a:
		f = 0
		for j in b:
			y = i & j | x
			if y == x:
				f = 1
				break
		if not f:
			break
	if f:
		print(x)
		break


def put():
	return map(int, input().split())
(n, m) = put()
a = list(put())
b = list(put())
ans = 0
for i in range(int(2000000000.0)):
	this = True
	for j in range(n):
		exist = False
		for k in range(m):
			if a[j] & b[k] & i == a[j] & b[k]:
				exist = True
				break
		if not exist:
			this = False
			break
	if this:
		ans = i
		break
print(ans)


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

def inpt():
	return [int(k) for k in input().split()]

def main():
	(n, m) = map(int, input().split())
	ar = inpt()
	br = inpt()
	for i in range(0, 513):
		ok = True
		for j in ar:
			sec = False
			for k in br:
				if i | j & k == i:
					sec = True
					break
			if sec == False:
				ok = False
				break
		if ok:
			print(i)
			break
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys, math, heapq, collections, itertools, bisect
sys.setrecursionlimit(101000)

def solve(n, m, a, b):
	seen = None
	for i in range(n):
		poss = [False] * 520
		for j in range(m):
			x = a[i] & b[j]
			poss[x] = True
		if seen:
			seen2 = [False] * 520
			for (c1, x1) in enumerate(seen):
				for (c2, x2) in enumerate(poss):
					if x1 and x2:
						seen2[c1 | c2] = True
			seen = seen2
		else:
			seen = poss
	return next((i for (i, x) in enumerate(seen) if x))
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(solve(n, m, a, b))


import math
import sys
import itertools
import random
import bisect
import heapq
import collections

def lcm(x, y):
	return x * y // math.gcd(x, y)

def hcf(x, y):
	return math.gcd(x, y)

def primes(n):
	prime = [True for _ in range(0, n + 1)]
	res = []
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[p] = False
		p += 1
	for i in range(0, n + 1):
		if prime[i] == True:
			res.append(i)
	return res

def primeFactors(n):
	res = []
	while n % 2 == 0:
		res.append(2)
		n //= 2
	for i in range(3, math.sqrt(n) + 1, 2):
		while n % i == 0:
			res.append(i)
			n //= i
	if n > 2:
		res.append(n)
	return res
(n, m) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
res = []
for i in a:
	res.append([])
	for j in b:
		res[-1].append([i & j, '0' * (9 - len(bin(i & j)[2:])) + bin(i & j)[2:]])
for i in range(0, 2 ** 9 + 1):
	g = 0
	for m1 in res:
		for n1 in m1:
			if n1[0] | i == i:
				g += 1
				break
	if g == n:
		print(i)
		break


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
for k in range(2 ** 9):
	solved = False
	for i in range(n):
		found = False
		for j in range(m):
			if a[i] & b[j] | k == k:
				found = True
				break
		if not found:
			break
		elif found and i == n - 1:
			print(k)
			solved = True
			break
	if solved:
		break


from sys import stdin
input = stdin.buffer.readline
(n, m) = map(int, input().split())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
d = [set(b) for i in range(n)]
ans = 0
for bit in range(9, -1, -1):
	s = set()
	for i in b:
		if not i >> bit & 1:
			s.add(i)
	for i in range(n):
		if a[i] >> bit & 1 and (not d[i] & s):
			ans += 1 << bit
			break
	else:
		for i in range(n):
			if a[i] >> bit & 1:
				d[i] &= s
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mat = [[0 for i in range(m)] for j in range(n)]
ans = 0
for i in range(n):
	for j in range(m):
		mat[i][j] = a[i] & b[j]
for i in range(513):
	aa = i
	f = True
	for j in range(n):
		c = False
		for k in range(m):
			if mat[j][k] | aa == aa:
				c = True
				break
		if c == False:
			f = False
			break
	if f == True:
		print(aa)
		break


from sys import stdin
input = stdin.readline
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mv = []
f = 0
for c in range(512):
	s = 0
	for i in a:
		f = 0
		for j in b:
			v = i & j | c
			if v == c:
				f = 1
				s += 1
				break
		if f == 0:
			break
	if s == n:
		print(c)
		break


def main():
	(n, m) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	A_ = [format(a, 'b').zfill(9) for a in A]
	B_ = [format(b, 'b').zfill(9) for b in B]
	dp = [0] * 2 ** 9
	dp[0] = 1
	import copy
	for i in range(n):
		a = A[i]
		temp = [0] * 2 ** 9
		for j in range(m):
			b = B[j]
			x = a & b
			for k in range(2 ** 9):
				if dp[k] == 1:
					temp[k | x] = 1
		dp = copy.copy(temp)
	for k in range(2 ** 9):
		if dp[k] == 1:
			print(k)
			exit()
main()


def bitop(n, m, a, b):
	for i in range(512):
		end = True
		for x in a:
			if all((x & y | i != i for y in b)):
				end = False
				break
		if end:
			return i
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(bitop(n, m, a, b))


(m, n) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
ands = []
for i in range(len(a)):
	curAnds = []
	for j in range(len(b)):
		curAnds.append(a[i] & b[j])
	ands.append(curAnds)
for i in range(2 ** 9):
	end = False
	for j in range(len(ands)):
		found = False
		for k in range(len(ands[j])):
			if ands[j][k] & i == ands[j][k]:
				found = True
		if found and j == len(ands) - 1:
			end = True
		elif found == False:
			break
	if end:
		print(i)
		break


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = float('inf')
for i in range(2 ** 9):
	t = 1
	for j in range(n):
		r = 0
		for k in range(m):
			v = a[j] & b[k]
			z = 1
			for l in range(9):
				if v >> l & 1 == 1 and i >> l & 1 == 0:
					z = 0
					break
			if z == 1:
				r = 1
				break
		if r == 0:
			t = 0
			break
	if t == 1:
		c = min(c, i)
print(c)


from sys import stdin
(n, m) = map(int, stdin.readline().rstrip().split(' '))
l1 = list(map(int, stdin.readline().rstrip().split(' ')))
l2 = sorted(list(map(int, stdin.readline().rstrip().split(' '))))
for x in range(0, 512):
	r = True
	for i in range(n):
		inside = False
		for j in range(m):
			y = x | l1[i] & l2[j]
			if y == x:
				inside = True
				break
		if not inside:
			r = False
			break
	if r:
		print(x)
		break


from sys import stdin, stdout
input = stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for ans in range(512):
	for i in a:
		f = 1
		for j in b:
			if i & j | ans == ans:
				f = 0
		if f:
			break
	else:
		break
print(ans)


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)

def solve():
	(n, m) = get_int_input_arr()
	a_arr = get_int_input_arr()
	b_arr = get_int_input_arr()
	for i in range(2 ** 9):
		can_do = True
		for j in range(n):
			a = a_arr[j]
			can_do_a = False
			for l in range(m):
				b = b_arr[l]
				if a & b | i == i:
					can_do_a = True
			if not can_do_a:
				can_do = False
		if can_do:
			cout << i << endl
			return

def main():
	solve()
main()


from collections import defaultdict
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for k in range(2 ** 9 + 1):
	ans = k
	for i in range(n):
		for j in range(m):
			if a[i] & b[j] | ans == ans:
				break
		else:
			break
	else:
		break
print(ans)


def check(x):
	for i in a:
		for j in b:
			if i & j | x == x:
				break
		else:
			return 0
	return 1
n = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
for ans in range(0, 2 ** 9):
	if check(ans):
		print(ans)
		break


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
(n, m) = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))

def check(val):
	for a in arr1:
		flag = 0
		for b in arr2:
			flag |= a & b | val == val
		if not flag:
			return 0
	return 1
for ans in range(512):
	if check(ans):
		print(ans)
		break


import sys
import math, bisect
from collections import defaultdict
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
(n, m) = neo()
A = Neo()
B = Neo()
for i in range(1024):
	t = 0
	for j in A:
		for k in B:
			if i | j & k == i:
				t += 1
				break
	if t == n:
		print(i)
		break


from sys import stdin
(la, lb) = [int(x) for x in stdin.readline().split()]
arr_a = [int(x) for x in stdin.readline().split()]
arr_b = [int(x) for x in stdin.readline().split()]
arr_a.sort(reverse=True)

def f(a, pn):
	if a == la:
		return pn
	ans = pn | arr_a[a] & arr_b[0]
	for bn in arr_b:
		temp = pn | arr_a[a] & bn
		if temp <= ans:
			ans = temp
	return f(a + 1, ans)
ans = f(0, 0)
for _ in range(la):
	v = arr_a.pop(0)
	arr_a.append(v)
	t = f(0, 0)
	if t < ans:
		ans = t
print(ans)


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
end = False
for i in range(512):
	for j in a:
		restart = True
		for k in b:
			if j & k | i == i:
				restart = False
				break
		if restart:
			break
	if restart:
		continue
	else:
		print(i)
		break


import sys
(n, m) = map(int, sys.stdin.readline().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [[x for x in b] for _ in range(n)]
sum = 0
for nob in range(9, -1, -1):
	count = 0
	se = set()
	for i in range(n):
		bit = a[i] >> nob
		if bit & 1 == 0:
			count += 1
			continue
		else:
			for j in c[i]:
				bit1 = j >> nob
				if bit1 & 1 == 0:
					se.add(i)
					count += 1
					break
	if count != n:
		sum = sum | 1 << nob
	else:
		for k in se:
			c[k] = [x for x in c[k] if not x >> nob & 1 == 1]
print(sum)


import sys
import math
input = sys.stdin.readline
(n, m) = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
C = [set() for i in range(n)]
for i in range(n):
	for j in range(m):
		c = A[i] & B[j]
		C[i].add(c)
D = [min(c) for c in C]
ans = 0
while True:
	power = max(D)
	if power == 0:
		break
	ans |= power
	for i in range(n):
		if D[i] == power:
			D[i] = 0
			continue
		if D[i] == 0:
			continue
		C[i] = {c - (c & power) for c in C[i]}
		D[i] = min(C[i])
print(ans)


(n, m) = map(int, input().split())
ln = list(map(int, input().split()))
lm = list(map(int, input().split()))
for ans in range(512):
	poss = True
	for i in ln:
		curr = False
		for j in lm:
			if i & j | ans == ans:
				curr = True
		if curr == False:
			poss = False
	if poss == True:
		print(ans)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for ans in range(512):
	cnt = 0
	for i in a:
		f = 0
		for j in b:
			x = ans | i & j
			if x <= ans:
				f = 1
		if f == 0:
			break
		else:
			cnt = cnt + 1
	if cnt == len(a):
		break
print(ans)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def main():
	(n, m) = map(int, inputi().split())
	aa = [int(a) for a in inputi().split()]
	bb = [int(a) for a in inputi().split()]
	res = 0
	while aa:
		besta = -1
		bestres = -1
		for a in aa:
			best = 2 ** 10
			for b in bb:
				best = min(a & b | res, best)
			if best > bestres:
				besta = a
				bestres = best
		res = bestres
		aa.remove(besta)
	print(res)
main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for k in range(2 ** 9 + 1):
	mainFlag = True
	for i in range(n):
		flag = False
		for j in range(m):
			if a[i] & b[j] & k == a[i] & b[j]:
				flag = True
				break
		if not flag:
			mainFlag = False
			break
	if mainFlag:
		print(k)
		break


import sys

def solve(A, B):
	zero_bits = [[] for _ in range(10)]
	for b in B:
		for i in range(10):
			mask = 1 << i
			if not b & mask:
				zero_bits[i].append(b)
	universes = [set(B) for _ in A]
	soln = 0
	A0 = list(A)
	for i in range(9, -1, -1):
		ok = True
		mask = 1 << i
		for (j, a0) in enumerate(A0):
			if a0 & mask:
				U0 = universes[j].intersection(zero_bits[i])
				if not U0:
					ok = False
		if ok:
			for (j, a0) in enumerate(A0):
				if a0 & mask:
					universes[j] = universes[j].intersection(zero_bits[i])
					A0[j] ^= mask
	soln = 0
	for a in A0:
		soln |= a
	return soln

def main():
	(N, M) = (int(i) for i in sys.stdin.readline().split())
	A = [int(i) for i in sys.stdin.readline().split()]
	B = set([int(i) for i in sys.stdin.readline().split()])
	print(solve(A, B))
main()


def readGenerator():
	while True:
		tokens = input().split(' ')
		for t in tokens:
			yield t
reader = readGenerator()

def readWord():
	return next(reader)

def readInt():
	return int(next(reader))

def readFloat():
	return float(next(reader))

def readLine():
	return input()

def solve(n):
	return n
n = readInt()
m = readInt()
a = [readInt() for _ in range(0, n)]
b = [readInt() for _ in range(0, m)]
for res in range(0, 2 ** 10):
	f = False
	for va in a:
		f = False
		for vb in b:
			if res | va & vb == res:
				f = True
				break
		if not f:
			break
	if f:
		print(res)
		break


from itertools import permutations
from functools import reduce

def f(l1, l2):
	ls = {i: [l1[i] & l2[j] for j in range(len(l2))] for i in range(len(l1))}
	rs = reduce(lambda l1, l2: {x | y for x in l1 for y in l2}, ls.values())
	return min(rs)
n = [int(i) for i in input().split()]
(first, second) = ([int(i) for i in input().split()], [int(i) for i in input().split()])
print(f(first, second))


(n, m) = map(int, input().split())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
ans = 0
for v in range(2 ** 9):
	ok = True
	for i in range(n):
		found = False
		for j in range(m):
			if v | a[i] & b[j] == v:
				found = True
		ok = ok & found
		if not ok:
			break
	if ok:
		ans = v
		break
print(ans)


temp = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [[False] * 2 ** 9 for i in range(len(a))]
for i in range(len(a)):
	for j in range(len(b)):
		if i == 0:
			dp[i][a[i] & b[j]] = True
		else:
			for k in range(2 ** 9):
				if dp[i - 1][k]:
					dp[i][k | a[i] & b[j]] = True
for i in range(2 ** 9):
	if dp[len(a) - 1][i]:
		print(i)
		break


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions
from collections import deque, defaultdict, OrderedDict
from fractions import Fraction
from decimal import Decimal

def yetohanswerhai(k, a, b):
	c = True
	for i in a:
		t = 0
		for j in b:
			if i & j | k == k:
				t += 1
				break
		if t == 0:
			c = False
			break
	return c

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(1):
		(n, m) = ria()
		a = ria()
		b = ria()
		for i in range(0, 2 ** 9 + 1):
			if yetohanswerhai(i, a, b):
				print(i)
				break
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = [int(i) for i in input().split() if i != '\n']
b = [int(i) for i in input().split() if i != '\n']
ok = False
for i in range(513):
	count = 0
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				count += 1
				break
		if count == n:
			print(i)
			ok = True
			break
	if ok:
		break


from math import *
t = 1
while t:
	t = t - 1
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	f = 0
	out = 512
	for i in range(513):
		f = 1
		for j in range(n):
			y = 0
			for k in range(m):
				temp = i | a[j] & b[k]
				if temp <= i:
					y = 1
					break
			if y == 0:
				f = 0
				break
		if f == 1:
			out = i
			break
	print(out)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
(n, m) = rm()
a = rl()
b = rl()
ans = []
for i in range(0, 2 ** 9):
	add = 0
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				add += 1
				break
	if add == n:
		print(i)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
r = []
for i in a:
	r.append([i & j for j in b])
d = dict()
l = r[0].copy()
for i in range(1, n):
	d = {}
	for j in range(m):
		for k in l:
			d[k | r[i][j]] = 1
	l = list(d.keys())
print(min(l))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def is_possible(candidate, a, b):
	for ai in a:
		found = False
		for bi in b:
			ci = ai & bi
			if ci | candidate == candidate:
				found = True
		if not found:
			return False
	return True
answer = 0
for candidate in range(0, 2 ** 10):
	if is_possible(candidate, a, b):
		answer = candidate
		break
print(answer)


def LI():
	return [int(i) for i in input().split()]
ans = 0
(n, m) = LI()
A = LI()
B = LI()
N = 9
dp = set([0])
for i in range(n):
	a = A[i]
	d = [a & j for j in B]
	nx = []
	for x in dp:
		nx += [j | x for j in d]
	dp = set(nx)
ans = min(dp)
print(ans)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict
(n, m) = ria()
x = ria()
y = ria()
for ans in range(512):
	flag = 1
	for i in range(n):
		z = 1
		for j in range(m):
			a = x[i] & y[j] | ans
			if a <= ans:
				z = 0
				break
		if z:
			flag = 0
			break
	if flag:
		break
wi(ans)


info = list(map(int, input('').split(' ')))
array1 = list(map(int, input('').split(' ')))
array2 = list(map(int, input('').split(' ')))
proc = dict(((elem1, [elem1 & elem2 for elem2 in array2]) for elem1 in array1))
for i in range(0, 2 ** 9):
	gflag = True
	for j in range(len(array1)):
		flag = False
		for k in range(len(array2)):
			if proc[array1[j]][k] | i == i:
				flag = True
				break
		if flag == False:
			gflag = False
			break
	if gflag == True:
		print(i)
		break


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
B = set(map(int, input().split()))

def check(ans):
	for a in A:
		all_c = [a & b for b in range(1 << 9) if b in B]
		if not any((c | ans == ans for c in all_c)):
			return False
	return True
for ans in range(0, 1 << 9):
	if check(ans):
		print(ans)
		break


def to_bits(x):
	ans = []
	for _ in range(9):
		ans.append(x % 2)
		x //= 2
	return tuple(ans[::-1])

def bigger(x):
	ans = set(x)
	while True:
		prev = len(ans)
		new = set(ans)
		for x in ans:
			for i in range(len(x)):
				if x[i] == 0:
					y = list(x)
					y[i] = 1
					y = tuple(y)
					new.add(y)
		ans = new
		if len(ans) == prev:
			break
	return ans

def main():
	(n, m) = list(map(int, input().split()))
	a = list(map(lambda x: to_bits(int(x)), input().split()))
	b = list(map(lambda x: to_bits(int(x)), input().split()))
	valid = bigger(b)
	for i in range(2 ** 9):
		bits = to_bits(i)
		for x in a:
			t = tuple((0 if i == 1 and j == 0 else 1 for (i, j) in zip(x, bits)))
			if t not in valid:
				break
		else:
			print(i)
			break
main()


from collections import defaultdict as dd
import math
import sys
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return list(map(int, input().split()))

def lm():
	return list(map(int, input().split()))
(n, m) = mi()
a_s = lm()
b_s = lm()
elim = []
for power in reversed(list(range(0, 10))):
	bad = False
	for a in a_s:
		found = False
		for b in b_s:
			b_bad = False
			for pre_pow in elim:
				if a & 1 << pre_pow and b & 1 << pre_pow:
					b_bad = True
			if a & 1 << power and b & 1 << power:
				b_bad = True
			if b_bad == False:
				found = True
		if not found:
			bad = True
			break
	if not bad:
		elim.append(power)
ans = 0
for i in range(0, 10):
	if i not in elim:
		ans += 2 ** i
print(ans)


def calc(i, cur):
	if i == n:
		return cur
	elif dp[i][cur] != -1:
		return dp[i][cur]
	answer = float('inf')
	for num in b:
		answer = min(answer, calc(i + 1, cur | a[i] & num))
	dp[i][cur] = answer
	return dp[i][cur]
lst = input().split(' ')
(n, m) = (int(lst[0]), int(lst[1]))
a = [int(num) for num in input().split(' ')]
b = [int(num) for num in input().split(' ')]
dp = [[-1 for j in range(2 ** 9)] for i in range(n)]
print(calc(0, 0))


from sys import stdin, stdout
for _ in range(1):
	(na, nb) = list(map(int, stdin.readline().split()))
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	bits = [[] for i in range(na)]
	for i in range(na):
		for j in range(nb):
			bits[i] += [a[i] & b[j]]
	ans = 0
	for num in range(1 << 9 + 1):
		this = 1
		for i in range(na):
			flag = 0
			for j in range(nb):
				if num | bits[i][j] == num:
					flag = 1
					break
			if flag == 0:
				this = 0
				break
		if this:
			ans = num
			break
	print(ans)


(n, m) = map(int, input().split())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
ans = int()
for x in range(pow(2, 9) + 1):
	flag = True
	for i in a:
		flag &= any((i & j | x == x for j in b))
	if flag:
		print(x)
		exit()


import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
(n, m) = nm()
a = nl()
b = nl()
a.insert(0, 0)
b.insert(0, 0)

def check(x):
	for i in range(1, n + 1):
		f = 0
		for j in range(1, m + 1):
			temp = a[i] & b[j]
			if temp | x == x:
				f = 1
				break
		if f == 0:
			return 0
	return 1
st = 0
for i in range(1, n + 1):
	st |= a[i]
for ans in range(st + 1):
	if check(ans):
		print(ans)
		break


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
k = {0}
for i in range(len(a)):
	t = set()
	for j in range(len(b)):
		an = a[i] & b[j]
		for v in k:
			t.add(an | v)
	k = t
print(min(k))


def val(num):
	chk = None
	for i in a:
		chk = False
		for j in b:
			if i & j | num == num:
				chk = True
				break
		if not chk:
			return chk
	return chk
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
(checker, OR) = (False, -1)
while not checker and OR < 512:
	OR += 1
	checker = val(OR)
print(OR)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *
input = stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = inf
for i in range(2 ** 9):
	c = 0
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				c += 1
				break
	if c == n:
		ans = min(ans, i)
print(ans)


import sys, threading
from heapq import heapify, heappop
sys.setrecursionlimit(10 ** 8)
threading.stack_size(10 ** 8)

def score(numa, numb, targetNum):
	mask = 1
	score = 0
	for i in range(9):
		res = bool(numa & mask and numb & mask)
		if targetNum & mask:
			score += res
		elif res:
			return float('-inf')
		mask <<= 1
	return score

def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for targetNum in range(2 ** 9):
		c = []
		for numa in a:
			maxScore = float('-inf')
			bestb = None
			for numb in b:
				scoree = score(numa, numb, targetNum)
				if scoree > maxScore:
					maxScore = scoree
					bestb = numb
			if bestb is None:
				break
			c.append(numa & bestb)
		else:
			out = 0
			for num in c:
				out |= num
			print(out)
			break
threading.Thread(target=main).start()


from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
from bisect import *
import copy
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
for i in range(513):
	ispossible = True
	for j in range(n):
		isfound = False
		for k in range(m):
			if arr[j] & brr[k] | i == i:
				isfound = True
				break
		if isfound == False:
			ispossible = False
			break
	if ispossible:
		print(i)
		break


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(sys.stdin.readline())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def Solution():
	(_, _) = get_ints_in_variables()
	a = get_ints_in_list()
	b = get_ints_in_list()
	for ans in range(pow(2, 9) + 1):
		isFound = True
		for v in a:
			flag = False
			for u in b:
				if u & v | ans == ans:
					flag = True
					break
			if not flag:
				isFound = False
				break
		if isFound:
			print(ans)
			break

def main():
	Solution()
main()


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def reduce_or(iterable):
	result = 0
	for item in iterable:
		result |= item
	return result

def reduce_and(iterable):
	result = (1 << 65) - 1
	for item in iterable:
		result &= item
	return result

def get_bit(mask: int, bit_length, index):
	return mask >> bit_length - index % bit_length - 1 & 1
MAX = 10
(n, m) = mpint()
aa = set(mpint())
bb = set(mpint())
data = [set((a & b for b in bb)) for a in aa]
ans = bit = 0
while bit <= MAX:
	if not any(data):
		break
	mask = reduce_or([reduce_and(st) for st in data])
	while get_bit(mask, MAX, bit):
		ans = (ans << 1) + 1
		bit += 1
	for st in data:
		for e in st.copy():
			if get_bit(e, MAX, bit):
				st.remove(e)
	bit += 1
	ans <<= 1
print(ans >> 1)


def bit_operations(n, m):
	for x in range(2 ** 9 + 1):
		flag = True
		for i in n:
			if any((i & j | x == x for j in m)):
				flag = True
			else:
				flag = False
				break
		if flag:
			return x
a = input()
lst1 = list(map(int, input().strip().split()))
lst2 = list(map(int, input().strip().split()))
print(bit_operations(lst1, lst2))


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = 1
	for _ in range(t):
		(n, m) = inar()
		arr = inar()
		arr2 = inar()
		ans = 0
		for i in range(2 ** 9):
			all = 0
			for j in range(n):
				counter = 0
				for k in range(m):
					if arr[j] & arr2[k] | i == i:
						counter = 1
						continue
				if counter == 0:
					all = 1
					break
			if all == 0:
				print(i)
				break
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def check(x):
	for i in range(n):
		done = False
		for j in range(m):
			if p[i] & q[j] | x == x:
				done = True
				break
		if not done:
			return 0
	return 1
(n, m) = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
for i in range(1 << 9):
	if check(i):
		print(i)
		break


input()
A = list(map(int, input().split()))
B = list(map(int, input().split()))
candidates = []
for a in A:
	candidates += [set((a & b for b in B))]

def has(cs, i):
	for c in cs:
		if c & 1 << i == 0:
			return True
	return False

def could_az(candidates, i):
	return all((has(cs, i) for cs in candidates))

def weeded(cs, i):
	ret = set(cs)
	for c in cs:
		if c & 1 << i != 0:
			ret.remove(c)
	assert len(ret) > 0
	return ret
ret = [1] * 11
for pos in range(10, -1, -1):
	if could_az(candidates, pos):
		ret[pos] = 0
		for i in range(len(candidates)):
			candidates[i] = weeded(candidates[i], pos)
ret = map(str, ret[::-1])
ret = ''.join(ret)
ret = int(ret, 2)
print(ret)


(n, m) = map(int, input().strip().split(' '))
a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
dp = [[0 for i in range(len(b))] for j in range(len(a))]
for i in range(len(a)):
	for j in range(len(b)):
		dp[i][j] = a[i] & b[j]
s = set(dp[0])
for i in range(1, len(a)):
	ss = set()
	for j in s:
		for k in range(len(b)):
			ss.add(dp[i][k] | j)
	s = ss
print(min(s))


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
initial_number = 1023
for shift in range(9, -1, -1):
	local_number = initial_number ^ 1 << shift
	suitable_numbers = 0
	for a_i in a:
		for b_i in b:
			if a_i & b_i <= a_i & b_i & local_number:
				suitable_numbers += 1
				break
	if n == suitable_numbers:
		initial_number = local_number
print(initial_number)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(int(pow(2, 9))):
	ans = i
	flag = True
	hold = [0] * n
	for j in range(n):
		for k in range(m):
			temp = a[j] & b[k]
			if temp & ~ans == 0:
				hold[j] = 1
				break
	for j in range(n):
		if hold[j] == 0:
			flag = False
			break
	if flag:
		print(ans)
		break


from sys import stdin, stdout, setrecursionlimit
from math import gcd, ceil, sqrt
from collections import Counter, deque
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
setrecursionlimit(100000)
mod = 1000000007
(n, m) = iia()
a = iia()
b = iia()
for i in range(513):
	for j in a:
		flag = False
		for k in b:
			if j & k | i == i:
				flag = True
		if not flag:
			break
	else:
		break
print(i)


import os
import sys
from io import BytesIO, IOBase
import threading
from math import factorial as fac
from collections import deque, defaultdict

def set_bits(num):
	bits = set()
	for i in range(9):
		if num & 1 << i:
			bits.add(i)
	return bits

def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	N = 2 ** 9
	for ans in range(N):
		ans_set = set_bits(ans)
		c = list(bytearray(n))
		tem_ans = set()
		for i in range(n):
			countj = 0
			for j in range(m):
				operation = a[i] & b[j]
				bits = set_bits(operation)
				if bits.issubset(ans_set):
					countj += 1
					c[i] = 1
					tem_ans.update(bits)
			if countj == 0:
				break
		if sum(c) == n and tem_ans == ans_set:
			print(ans)
			exit(0)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def ispos(num, a, b):
	for i in a:
		pos = False
		for j in b:
			val = i & j
			if val | num == num:
				pos = True
				break
		if pos == False:
			return False
	return True
(n, m) = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
b = [int(c) for c in input().split()]
c = []
for i in range(513):
	if ispos(i, a, b):
		print(i)
		break


import sys, math, cmath, time
start_time = time.time()

def solve():
	(n, m) = invr()
	a = inlt()
	b = inlt()
	qw = 0
	for i in range(2 ** 9 + 1):
		q = 0
		for j in range(len(a)):
			for k in range(len(b)):
				if a[j] & b[k] | i == i:
					q = q + 1
					break
			if q == len(a):
				w = i
				qw = 1
				break
		if qw == 1:
			break
	print(w)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


(n, m) = map(int, input().split())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
t = [[] for i in range(n)]
for i in range(n):
	for j in range(m):
		t[i].append(a[i] & b[j])
for j in range(2 ** 9):
	f = 1
	for i in range(n):
		flag = 0
		for x in t[i]:
			if j | x == j:
				flag = 1
				break
		if flag == 0:
			f = 0
			break
	if f == 1:
		print(j)
		break


def found(i, j):
	for k in j:
		if k | i <= i:
			return True
	return False
(n, m) = map(int, input().split())
a = list(set(list(map(int, input().split()))))
b = list(set(list(map(int, input().split()))))
c = []
for i in a:
	c.append([])
	for j in b:
		c[-1].append(i & j)
for i in range(1024):
	flag = 0
	for j in c:
		if not found(i, j):
			flag = 1
			break
	if not flag:
		ans = i
		break
print(ans)


import math
from collections import Counter
import math
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
ls = list(map(int, input().split()))
for i in range(2 ** 9 + 1):
	cnt = 0
	for j in range(n):
		f = 0
		for k in range(m):
			if i | arr[j] & ls[k] == i:
				f = 1
				break
		if f == 0:
			break
		else:
			cnt += 1
	if cnt == n:
		print(i)
		break


(n, m) = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = (1 << 9) - 1
b_for = {a_i: b.copy() for a_i in a}
for bit in (1 << i for i in reversed(range(9))):
	b_for_zero = dict()
	for (a_i, b) in b_for.items():
		b_zero = [b_i for b_i in b if a_i & b_i & bit == 0]
		if b_zero:
			b_for_zero[a_i] = b_zero
		else:
			break
	else:
		b_for = b_for_zero
		c ^= bit
print(c)


import sys
read = sys.stdin.readline
write = sys.stdout.write
(n, m) = map(int, read().split())
a = set(map(int, read().split()))
b = set(map(int, read().split()))
for dest in range(0, 513):
	possible = 1
	for i in a:
		check = 0
		for j in b:
			if i & j | dest == dest:
				check = 1
		if check == 0:
			possible = 0
	if possible == 1:
		print(dest)
		break


def check(a, b, x):
	for i in a:
		flg = False
		for j in b:
			if i & j | x == x:
				flg = True
				break
		if not flg:
			return False
	return True

def proC(a, b):
	for i in range(1 << 9):
		if check(a, b, i):
			return i
arr = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(proC(a, b))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
candidates = []
for _ in range(n):
	candidates.append(b.copy())
c = 0
for i in range(8, -1, -1):
	important = True
	for j in range(n):
		if a[j] & 1 << i > 0:
			if all(map(lambda x: x & 1 << i > 0, candidates[j])):
				important = False
	if important:
		for j in range(n):
			if a[j] & 1 << i > 0:
				candidates[j] = list(filter(lambda x: x & 1 << i == 0, candidates[j]))
	else:
		c |= 1 << i
print(c)


MOD = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
from collections import *
(n, m) = f()
a = il()
b = il()
c = 0
for i in range(2 ** 9):
	fl = 1
	for j in range(0, n):
		f = 0
		for k in range(0, m):
			if a[j] & b[k] | i == i:
				f = 1
				break
		if f == 0:
			fl = 0
			break
	if fl == 1 and c == 0:
		print(i)
		c = 1
		break


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
if 0 in b:
	print(0)
else:
	alle = [[i & j for j in b] for i in a]
	ans = 0
	for i in range(513):
		cnt = 0
		for j in range(n):
			for k in range(m):
				if alle[j][k] | i <= i:
					cnt += 1
					break
		if cnt >= n:
			ans = i
			break
	print(ans)


temparr = input()
temparr = temparr.split()
n = int(temparr[0])
m = int(temparr[1])
arra = []
arrb = []
temparr = input()
temparr = temparr.split()
for i in temparr:
	arra.append(int(i))
temparr = input()
temparr = temparr.split()
for i in temparr:
	arrb.append(int(i))
import math
upperbound = int(math.pow(2, 9) + 1)
for ans in range(0, upperbound):
	count = 0
	for i in arra:
		flagj = 0
		for j in arrb:
			ii = i & j
			if ii == ans or ii | ans == ans:
				flagj = 1
				break
		if flagj == 0:
			break
		count += 1
	if count == n:
		break
print(ans)


import math
import sys
import bisect
input = sys.stdin.readline
t = 1
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [[0 for j in range(m)] for i in range(n)]
	ans = [0] * 10
	ind = {}
	for i in range(n):
		ind[i] = []
	for i in range(n):
		for j in range(m):
			c[i][j] = a[i] & b[j]
			ind[i].append(j)
	ind1 = {}
	for i in range(n):
		ind1[i] = []
	for i1 in range(9, -1, -1):
		cnt = 0
		ind1 = {}
		for i in range(n):
			ind1[i] = []
			for j in range(len(ind[i])):
				k = c[i][ind[i][j]] >> i1
				if k & 1 == 0:
					ind1[i].append(ind[i][j])
			if len(ind1[i]) > 0:
				cnt += 1
		if cnt == n:
			ind = {}
			for i in range(n):
				ind[i] = []
				for j in range(len(ind1[i])):
					ind[i].append(ind1[i][j])
		else:
			ans[i1] = 1
	suma = 0
	mul = 1
	for i in range(10):
		suma += mul * ans[i]
		mul *= 2
	print(suma)


def get_ints():
	return list(map(int, list(input().split())))
(n, m) = get_ints()
ns = get_ints()
ms = get_ints()
for ret in range(0, 2 ** 9):
	works = True
	for i in ns:
		found = False
		for j in ms:
			if i & j | ret == ret:
				found = True
				break
		if not found:
			works = False
			break
	if works:
		print(ret)
		break


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
pairs = [[True for i in range(m)] for j in range(n)]
ans = 0
for i in range(8, -1, -1):
	const = 2 ** i
	prob = False
	for j in range(n):
		act_prob = True
		for l in range(m):
			if pairs[j][l] and a[j] & b[l] & const == 0:
				act_prob = False
		if act_prob:
			prob = True
	if prob:
		ans += const
	else:
		for j in range(n):
			for l in range(m):
				if pairs[j][l] and a[j] & b[l] & const > 0:
					pairs[j][l] = False
print(ans)


import io
import os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

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

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())

def can_del(ai, p):
	for i in range(m):
		if a[ai] & b[i] & 2 ** p == 0 and i in aset[ai]:
			return True
	else:
		return False

def add_set(ai, p):
	new_set = set()
	for i in range(m):
		if a[ai] & b[i] & 2 ** p == 0 and i in aset[ai]:
			new_set.add(i)
	aset[ai] = new_set
(n, m) = rint()
a = list(rint())
b = list(rint())
aset = [set([j for j in range(m)]) for i in range(n)]
ans = 0
for i in range(9, -1, -1):
	for j in range(n):
		if not can_del(j, i):
			ans |= 2 ** i
			break
	else:
		for j in range(n):
			add_set(j, i)
print(ans)


(n, m) = [int(i) for i in input().split()]
lst1 = [int(i) for i in input().split()]
lst2 = [int(i) for i in input().split()]
for k in range(512):
	check = True
	for i in range(n):
		flag = False
		for j in range(m):
			if lst1[i] & lst2[j] | k == k:
				flag = True
				break
		if not flag:
			check = False
			break
	if check:
		print(k)
		break


from sys import stdin
from collections import defaultdict
input = stdin.readline
(n, m) = list(map(int, input().rstrip().split(' ')))
a = list(map(int, input().rstrip().split(' ')))
b = list(map(int, input().rstrip().split(' ')))
output = [0] * 10
available = [[i for i in range(m)] for j in range(n)]
for k in range(9, -1, -1):
	tempAvailable = [[] for i in range(n)]
	notPoss = False
	for i in range(n):
		for j in available[i]:
			if 1 << k & a[i] & b[j] == False:
				tempAvailable[i].append(j)
		if len(tempAvailable[i]) == 0:
			output[k] = 1
			notPoss = True
			break
	if not notPoss:
		available = tempAvailable
answer = 0
for i in range(10):
	if output[i] == 1:
		answer += 1 << i
print(answer)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s)
	sys.stdout.write('\n')

def wi(n):
	sys.stdout.write(str(n))
	sys.stdout.write('\n')

def wia(a, sep=' '):
	sys.stdout.write(sep.join([str(x) for x in a]))
	sys.stdout.write('\n')

def main():
	(n, m) = ria()
	a = ria()
	b = ria()
	t = [[0] * m for _ in range(n)]
	for i in range(n):
		for j in range(m):
			t[i][j] = a[i] & b[j]
	for ans in range(0, 513):
		ok = True
		for i in range(n):
			j_exists = False
			for j in range(m):
				if t[i][j] | ans == ans:
					j_exists = True
					break
			if not j_exists:
				ok = False
				break
		if ok:
			wi(ans)
			return
main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def possible(x):
	for i in range(n):
		ok = False
		for j in range(m):
			x1 = a[i] & b[j]
			f = 0
			for k in range(mx):
				if x1 & 1 << k and x[k] == 0:
					f = 1
					break
			if f == 0:
				ok = True
				break
		if ok == False:
			return 0
	return 1
mx = len(bin(max(a))[2:])
for i in range(1 << mx):
	x = [0] * mx
	for j in range(mx):
		if i & 1 << j:
			x[j] = 1
	if possible(x):
		print(i)
		exit(0)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
(n, m) = ip()
a = ip()
b = ip()
ans = -1
for k in range(513):
	mf = 1
	for i in a:
		flag = 1
		for j in b:
			if k & (i & j) == i & j:
				flag = 0
				break
		if flag:
			mf = 0
			break
	if mf:
		ans = k
		break
print(ans)


import sys
import heapq
import math
from collections import deque
from collections import defaultdict
from collections import Counter
modH = int(10 ** 9) + 7
I = lambda : list(map(int, input().split(' ')))

def PRINT(lst, sep=' '):
	print(sep.join(map(str, lst)))
(n, m) = I()
a = I()
b = I()

def isVal(num, a, b):
	(n, m) = (len(a), len(b))
	for i in range(n):
		s = 0
		for j in range(m):
			if num | a[i] & b[j] == num:
				s = 1
				break
		if s == 0:
			return False
	return True
for val in range(512):
	if isVal(val, a, b):
		print(val)
		break


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = []
for i in range(n):
	c = []
	for j in range(m):
		c.append(a[i] & b[j])
	list(set(c)).sort()
	d.append(c)
for i in range(512):
	e = []
	c = 0
	for j in range(n):
		for k in range(m):
			if d[j][k] | i == i:
				e.append(i)
				c += 1
				break
	if c == n and len(set(e)) == 1:
		print(e[0])
		break


import collections

def f(a, b):
	for ans in range(0, 512):
		for i in range(len(a)):
			flag = False
			for j in range(len(b)):
				if a[i] & b[j] | ans == ans:
					flag = True
					break
			if not flag:
				break
		if flag:
			return ans
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
print(f(a, b))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
for i in range(2 ** 9):
	cnt = 0
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				cnt += 1
				I = i
				break
	if cnt == n:
		print(I)
		break


def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, m) = mi()
a = li()
b = li()

def fun(x):
	for i in range(n):
		ok = False
		for j in range(m):
			x1 = a[i] & b[j]
			f = 0
			for k in range(mx):
				if x1 & 1 << k and x & 1 << k == 0:
					f = 1
					break
			if f == 0:
				ok = True
				break
		if ok == False:
			return 0
	return 1
mx = len(bin(max(a))[2:])
for i in range(1 << mx):
	x = 0
	for j in range(mx):
		if i & 1 << j:
			x |= 1 << j
	if fun(x):
		print(i)
		exit(0)


from sys import stdin, stdout
import bisect
import math
from collections import deque
mod = 10 ** 9 + 7

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def DFS(dictionary, vertex, visited):
	visited[vertex] = True
	stack = [vertex]
	print(vertex)
	while stack:
		a = stack.pop()
		for i in dictionary[a]:
			if not visited[i]:
				print(i)
				visited[i] = True
				stack.append(i)

def BFS(dictionary, vertex, visited):
	visited[vertex] = True
	q = deque()
	q.append(vertex)
	while q:
		a = q.popleft()
		for i in dictionary[a]:
			if not visited[i]:
				visited[i] = True
				q.append(i)
				print(i)

def soe(limit):
	l = [1] * (limit + 1)
	l[0] = 0
	l[1] = 0
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		a = a % b
		(b, a) = (a, b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def solve():
	(n, m) = mp()
	l = li()
	k = li()
	for i in range(1 << 9):
		x = True
		for j in l:
			y = False
			for v in k:
				a = j & v
				if i >= a and i | a == i:
					y = True
					break
			if not y:
				x = False
				break
		if x:
			pr(i)
			return
for _ in range(1):
	solve()


(n, m) = list(map(int, input().split()))
ls_a = list(map(int, input().split()))
ls_b = list(map(int, input().split()))
check = True
curr_or = 0
prev_ls = []
while check:
	min_ls = []
	for i in ls_a:
		mini = None
		for j in ls_b:
			if mini is None or i & j | curr_or < mini:
				mini = i & j | curr_or
		min_ls.append(mini)
	if curr_or == max(min_ls):
		check = False
	else:
		curr_or = max(min_ls)
print(curr_or)


import math
import os
from io import BytesIO, IOBase
import sys
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c_pos = []
for (idx, val) in enumerate(a):
	c_pos.append([val & x for x in b])
res = [[0 for i in range(512)] for j in range(len(a))]
for val in c_pos[0]:
	res[0][val] = 1
for i in range(n - 1):
	for j in range(512):
		if res[i][j] == 0:
			continue
		for k in range(m):
			res[i + 1][j | c_pos[i + 1][k]] = 1
for i in range(512):
	if res[-1][i] == 1:
		print(i)
		break


(n, m) = map(int, input().split(' '))
aa = list(map(int, input().split(' ')))
bb = list(map(int, input().split(' ')))
mbits = 9
absentMask = 0
for bit in reversed(range(mbits)):
	mask = absentMask | 1 << bit
	good = True
	for a in aa:
		found = False
		for b in bb:
			c = a & b
			if c & mask == 0:
				found = True
				break
		if not found:
			good = False
			break
	if good:
		absentMask = mask
print(2 ** mbits - 1 - absentMask)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
ans = -1
for x in range(0, 2 ** 9 + 1):
	k1 = bin(x)[2:]
	flag1 = 0
	for i in range(n):
		flag = 0
		for j in range(m):
			z = l1[i] & l2[j]
			if z | x == x:
				flag = 1
				break
		if flag == 0:
			flag1 = 1
			break
	if flag1 == 0:
		print(x)
		break


from sys import stdin, stdout
from math import gcd, sqrt, factorial
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7

def check(i):
	for p in a:
		flg = False
		for k in b:
			if p & k | i == i:
				flg = True
		if not flg:
			return flg
	return True
(m, n) = R()
a = L()
b = L()
for i in range(2 ** 9):
	if check(i):
		exit(print(i))


import os
import sys
from io import BytesIO, IOBase
import threading
from collections import defaultdict
import heapq
from math import ceil, floor

def main():
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	ans = -1
	for mask in range(2 ** 9):
		bol = True
		for i in arr:
			b = False
			for j in brr:
				if ~mask & (i & j) == 0:
					b = True
					break
			if not b:
				bol = False
		if bol:
			ans = mask
			break
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
minn = 10 ** 9
for ans in range(2 ** 9):
	t = True
	for i in a:
		g = False
		for j in b:
			if i & j | ans == ans:
				g = True
				break
		t = t and g
	if t:
		minn = min(minn, ans)
print(minn)


import sys, heapq as h
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math

def works(el, arr, ans):
	for el2 in arr:
		if el & el2 | ans == ans:
			return True
	return False

def solve():
	(N, M) = getInts()
	A = getInts()
	B = getInts()
	for ans in range(2 ** 9):
		flag = True
		for i in range(N):
			if not works(A[i], B, ans):
				flag = False
				break
		if flag:
			return ans
print(solve())


from sys import stdin, stdout

def boboniu_and_bit_operations(n, m, a_a, b_a):
	for k in range(513):
		cnt = 0
		for a in a_a:
			for b in b_a:
				if a & b | k == k:
					cnt += 1
					break
		if cnt == n:
			return k
	return -1
(n, m) = map(int, stdin.readline().split())
a_a = list(map(int, stdin.readline().split()))
b_a = list(map(int, stdin.readline().split()))
stdout.write(str(boboniu_and_bit_operations(n, m, a_a, b_a)) + '\n')


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for pw in range(9, -1, -1):
	x = 2 ** pw
	p = []
	for i in range(n):
		if a[i] & x > 0:
			p.append(a[i])
	if len(p) == 0:
		continue
	for j in range(m):
		if b[j] & x != 0:
			continue
		ok = False
		for ai in p:
			if (ai & b[j] | ans) - ans > x:
				break
		else:
			break
	else:
		ans += x
print(ans)


import sys
from collections import defaultdict
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(512):
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				break
		else:
			break
	else:
		print(i)
		break


import sys
import math
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(n, m) = invr()
a = inara()
b = inara()
for i in range(512):
	koyta = 0
	for j in range(n):
		for k in range(m):
			if i | a[j] & b[k] == i:
				koyta += 1
				break
	if koyta == n:
		print(i)
		exit(0)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
(n, m) = sp()
a = l()
b = l()
power = 1 << 9
for i in range(power):
	cnt = 0
	for j in range(n):
		for k in range(m):
			if a[j] & b[k] | i == i:
				cnt += 1
				break
	if cnt == n:
		out(i)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(512):
	flag1 = 0
	for ai in a:
		flag2 = 0
		for bi in b:
			if ai & bi | i == i:
				flag2 = 1
				break
		if flag2 == 0:
			flag1 = 1
			break
	if flag1 == 0:
		print(i)
		break


def func(i):
	global n, m
	ans = -1
	for A in range(513):
		count = 0
		for i in range(n):
			for j in range(m):
				if a[i] & b[j] | A == A:
					count += 1
					break
		if count == n:
			ans = A
			break
	print(ans)
(n, m) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
func(0)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
(n, m) = RL()
a = RLL()
b = RLL()
data = []
mb = min(b)
if mb == 0:
	print(0)
else:
	for i in range(n):
		data.append(set())
		for j in range(m):
			tmp = a[i] & b[j]
			if tmp == 0:
				data.pop()
				break
			elif tmp <= mb:
				data[-1].add(tmp)
	for d in data:
		tmp = list(d)
		l = len(tmp)
		for i in range(l):
			for j in range(i + 1, l):
				if tmp[i] & tmp[j] == tmp[i]:
					d -= {tmp[j]}
				elif tmp[i] & tmp[j] == tmp[j]:
					d -= {tmp[i]}
	c = 0
	t = mb
	while t:
		t >>= 1
		c += 1
	res = 0
	for i in range(c, 0, -1):
		f = True
		now = 1 << i - 1
		for d in data:
			ff = True
			for k in d:
				if k & now == 0:
					ff = False
					break
			if ff:
				f = False
				break
		if not f:
			res += now
		else:
			tmp = []
			for d in data:
				tmp.append([zz for zz in d if zz & now == 0])
			data = tmp
	print(res)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for S in range(2 ** 9):
	for i in range(n):
		for j in range(m):
			if S | a[i] & b[j] == S:
				break
		else:
			break
	else:
		print(S)
		exit()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = -1
for z in range(2 ** 9):
	for i in range(n):
		k = False
		for bj in b:
			if bj & a[i] | z == z:
				k = True
				break
		if not k:
			break
		if i == n - 1:
			ans = z
	if ans != -1:
		break
print(ans)


import math
import sys
from collections import defaultdict
from functools import lru_cache

def check(num):
	for i in range(n):
		flag = 0
		for j in range(m):
			c = a[i] & b[j]
			if c | num == num:
				flag = 1
		if flag == 0:
			return 0
	return 1
(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
for i in range(0, 513):
	if check(i):
		print(i)
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	for j in range(m):
		dp[i][j] = a[i] & b[j]
ans = float('inf')
l = 1 << 9
for i in range(l):
	sums = 0
	for j in range(n):
		for k in range(m):
			if i | dp[j][k] == i:
				sums += 1
				break
	if sums == n:
		print(i)
		break
else:
	print(511)


from math import log2
import sys
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
inf = float('inf')
l = [i for i in range(pow(2, 9))]
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mina = inf
for A in l:
	found = 1
	for i in a:
		found1 = 0
		for j in b:
			if i & j | A == A:
				found1 = 1
				break
		found = min(found, found1)
		if found == 0:
			break
	if found:
		print(A)
		break


(n, m) = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]
all_a = []
for aa in a:
	t = []
	for bb in b:
		t.append(aa & bb)
	all_a.append(t)

def get_bit(n, k):
	return (n & 1 << k) >> k
final = 0
for i in range(8, -1, -1):
	can_get_zero = True
	for a in all_a:
		can_get_zero_in_a = False
		for aa in a:
			if get_bit(aa, i) == 0:
				can_get_zero_in_a = can_get_zero_in_a or True
		can_get_zero = can_get_zero and can_get_zero_in_a
	if can_get_zero:
		for j in range(len(all_a)):
			t = []
			for aa in all_a[j]:
				if get_bit(aa, i) == 0:
					t.append(aa)
			all_a[j] = t
	else:
		final = final + (1 << i)
print(final)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def check(target):
	res = 1
	for ai in a:
		ok = 0
		for bi in b:
			ci = ai & bi
			if ci | target == target:
				ok = 1
		if ok == 0:
			res = 0
			break
	return res
ans = 2 ** 10
for i in range(2 ** 10):
	if check(i):
		ans = i
		break
print(ans)


from collections import deque as dq
from collections import defaultdict as dd
from collections import Counter as ct
from functools import lru_cache as lc
from heapq import heappush as hpush, heappop as hpop, heapify as hfy
from bisect import bisect_left, bisect_right
import itertools as it
import math
import sys
input = sys.stdin.readline

def solve():
	(N, M) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	C = [[] for i in range(N)]
	for i in range(N):
		for j in range(M):
			C[i].append(A[i] & B[j])

	def need(p):
		for i in range(N):
			x = (1 << 10) - 1
			for j in range(M):
				if C[i][j] == -1:
					continue
				x &= C[i][j]
			if x & 1 << p:
				return True
		for i in range(N):
			for j in range(M):
				if C[i][j] & 1 << p:
					C[i][j] = -1
		return False
	ret = 0
	for i in range(9, -1, -1):
		if need(i):
			ret |= 1 << i
	print(ret)
t = 1
for i in range(t):
	solve()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
sys.setrecursionlimit(300000)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = defaultdict(list)
d1 = defaultdict(list)
for i in range(m):
	c = bin(b[i])[2:]
	c = '0' * (9 - len(c)) + c
	for j in range(9):
		if c[j] == '0':
			d[9 - j].append(b[i])
for i in range(n):
	c = bin(a[i])[2:]
	c = '0' * (9 - len(c)) + c
	for j in range(9):
		if c[j] == '1':
			d1[9 - j].append(a[i])
cur = b + []
ans = 0
cost = set()
for i in range(9, 0, -1):
	if len(d1[i]) == 0:
		continue
	else:
		for j in d1[i]:
			e = set(b)
			c = bin(j)[2:]
			c = '0' * (9 - len(c)) + c
			for k in range(9, i - 1, -1):
				if c[9 - k] == '1' and k not in cost:
					e = e & set(d[k])
			if len(e) == 0:
				ans += pow(2, i - 1)
				cost.add(i)
				break
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(513):
	found_ans = True
	for j in range(n):
		ok = False
		for k in range(m):
			if a[j] & b[k] | i == i:
				ok = True
		if not ok:
			found_ans = False
			break
	if found_ans:
		print(i)
		break


(n, m) = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
ans = 0
i = 0
dp1 = {0}
for a in arr1:
	dp2 = set()
	for b in arr2:
		for d in dp1:
			dp2.add(d | a & b)
	dp1 = dp2
print(min(dp1))


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mv = []
f = 0
for c in range(512):
	s = 0
	for i in a:
		f = 0
		for j in b:
			v = i & j | c
			if v == c:
				f = 1
				s += 1
				break
		if f == 0:
			break
	if s == n:
		print(c)
		break


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(0, 2 ** 9 + 1):
	ans = i
	for j in range(len(a)):
		elm = a[j]
		for k in range(len(b)):
			if elm & b[k] | ans == ans:
				break
		else:
			break
	else:
		break
print(ans)


line = input().split(' ')
n = int(line[0])
m = int(line[1])
arr1 = []
line2 = input().split(' ')
for a in line2:
	arr1.append(int(a))
arr2 = []
line3 = input().split(' ')
for a in line3:
	arr2.append(int(a))
endpt = 2 ** 9
ans = endpt
for i in range(endpt):
	count = 0
	for n1 in arr1:
		for n2 in arr2:
			if n1 & n2 | i == i:
				count += 1
				break
	if count == len(arr1):
		ans = i
		break
print(ans)


from itertools import product
(n, m) = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
abl = []
for a in al:
	arow = []
	for b in bl:
		ab = a & b
		arow.append(ab)
	abl.append(arow)
ite = list(product(range(2), repeat=9))
for (orval, pattern) in enumerate(ite):
	zeros = []
	for (i, v) in enumerate(pattern):
		if v == 0:
			zeros.append(9 - i - 1)
	cnt = 0
	for arow in abl:
		for ab in arow:
			for zero_i in zeros:
				if ab & 1 << zero_i > 0:
					break
			else:
				cnt += 1
				break
	if cnt == n:
		print(orval)
		break


(x, y) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for n in range(1 << 9 + 1):
	ct = 0
	for i in a:
		for j in b:
			if n | i & j == n:
				ct += 1
				break
	if ct == x:
		print(n)
		exit(0)


from math import *
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
d = list(map(int, input().split()))
for i in range(2 ** 9):
	c = 1
	for j in range(n):
		f = 0
		for k in range(m):
			if l[j] & d[k] | i == i:
				f = 1
				break
		if f == 0:
			c = 0
			break
	if c:
		print(i)
		break


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for k in range(513):
	z = 0
	for i in range(n):
		for j in range(m):
			x = a[i] & b[j]
			if x | k == k:
				z += 1
				break
		else:
			break
	if z == n:
		print(k)
		break


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().split())
inl = lambda : list(map(int, input().split()))
(n, m) = inm()
a = inl()
b = inl()
for x in range(512):
	outt = True
	for i in a:
		out = False
		for j in set(b):
			if i & j | x == x:
				out = True
				break
		if out == False:
			outt = False
	if outt:
		print(x)
		break


(na, nb) = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
ans = 0
for i in range(512):
	hflag = True
	for j in a:
		flag = False
		for k in b:
			if j & k | i == i:
				flag = True
				break
		hflag = hflag and flag
	if hflag:
		ans = i
		break
print(ans)


(n, m) = [int(n) for n in input().split()]
arr = [int(item) for item in input().split()]
brr = [int(item) for item in input().split()]
for ans in range(2 ** 9 + 1):
	ok = 1
	for i in range(n):
		mil = False
		for j in range(m):
			if arr[i] & brr[j] | ans == ans:
				mil = True
				break
		if mil == True:
			continue
		if mil == False:
			ok = 0
			break
	if ok == 1:
		break
print(ans)


import sys
input = sys.stdin.buffer.readline
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
pa = [[-1 for __ in range(m)] for _ in range(n)]
for i in range(n):
	for j in range(m):
		pa[i][j] = a[i] & b[j]
memo = [set() for _ in range(n)]

def dp(i):
	if i == -1:
		return [0]
	if memo[i] == set():
		res = set()
		for j in range(m):
			for xx in dp(i - 1):
				res.add(xx | pa[i][j])
		memo[i] = res
	return memo[i]
for i in range(n):
	dp(i)
print(min(dp(n - 1)))


def gns():
	return list(map(int, input().split()))
(n, m) = gns()
ns = gns()
ms = gns()
ans = [set(ms) for i in range(n)]

def gt(x, i):
	return x >> i & 1 == 1

def check(loc, i):
	rt = set()
	for x in ans[loc]:
		if not gt(x, i):
			rt.add(x)
	return rt

def check_bit(i):
	for loc in range(n):
		if not gt(ns[loc], i):
			continue
		rt = check(loc, i)
		if len(rt) == 0:
			return False
	return True
for i in reversed(range(11)):
	if not check_bit(i):
		continue
	for loc in range(n):
		if not gt(ns[loc], i):
			continue
		ans[loc] = check(loc, i)
for c in ans[0]:
	x = ns[0] & c
	break
for i in range(1, n):
	for c in ans[i]:
		c = c & ns[i]
		x = x | c
		break
print(x)


import sys
input = sys.stdin.readline

def main():
	(N, M) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	B = [int(x) for x in input().split()]
	ans = 0
	for i in range(513):
		f = True
		for a in A:
			for b in B:
				if a & b & i - (a & b) == 0:
					break
			else:
				f = False
			if not f:
				break
		if f:
			print(i)
			return
main()


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
i = 0
while True:
	cnt = 0
	for j in A:
		for k in B:
			if j & k | i == i:
				cnt += 1
				break
	if cnt == n:
		break
	i += 1
print(i)


(a, b) = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
flag = 0
for bit in range(513):
	for i in range(a):
		flag = 0
		for j in range(b):
			if A[i] & B[j] & ~bit == 0:
				flag = 1
				break
		if flag == 0:
			break
	if flag:
		print(bit)
		break


[n, m] = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for ans in range(512):
	check = True
	for x in a:
		check1 = False
		for y in b:
			if x & y | ans == ans:
				check1 = True
		if not check1:
			check = False
			break
	if check:
		print(ans)
		break


production = True
import sys, math, collections, bisect, itertools, heapq, decimal, random, copy, re

def input(f=0, m=0):
	if m > 0:
		return [input(f) for i in range(m)]
	else:
		l = sys.stdin.readline()[:-1]
		if f >= 10:
			u = False
			f = int(str(f)[-1])
		else:
			u = True
		if f == 0:
			p = [l]
		elif f == 1:
			p = list(map(int, l.split()))
		elif f == 2:
			p = list(map(float, l.split()))
		elif f == 3:
			p = list(l)
		elif f == 4:
			p = list(map(int, list(l)))
		elif f == 5:
			p = l.split()
		return p if u else p[0]

def out(l, f=0, n=True):
	if f == 0:
		p = str(l)
	elif f == 1:
		p = ' '.join(map(str, l))
	elif f == 2:
		p = '\n'.join(map(str, l))
	elif f == 3:
		p = ''.join(map(str, l))
	print(p, end='\n' if n else '')

def log(*args):
	if not production:
		print('$$$', end='')
		print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)

def mapl(i, f=0):
	if f == 0:
		return list(map(int, i))
	elif f == 1:
		return list(map(str, i))
	elif f == 2:
		return list(map(list, i))

def solve():
	(n, m) = input(1)
	a = input(1)
	b = input(1)
	k = []
	for i in a:
		c = set()
		for j in b:
			c.add(i & j)
		k.append(c)
	for i in k:
		log(i)
	for i in range(0, 513):
		f = 0
		for j in k:
			for q in j:
				if q | i == i:
					break
			else:
				f = 1
		if f:
			continue
		else:
			out(i)
			break
	log()
	return
solve()


def chec(x, n, m, a, b):
	for i in range(n):
		flag = False
		for j in range(m):
			if a[i] & b[j] | x == x:
				flag = True
				break
		if flag == False:
			return False
	return True
(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
for i in range(0, 1 << 10):
	if chec(i, n, m, a, b):
		print(i)
		break


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 1000000007

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = []
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > 1:
		factors.append(n)
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def most_set_bits_value(n1, n2):
	b1 = bin(n1)[-1:1:-1]
	b2 = bin(n2)[-1:1:-1]
	value = 0
	for i in range(min(len(b1), len(b2))):
		if b1[i] == b2[i]:
			value += 2 ** i
	return value

def main():
	(n, m) = vars()
	a = array()
	b = array()
	i = 0
	while i < 2 ** 9:
		cnt = 0
		for num1 in a:
			for num2 in b:
				if num1 & num2 | i == i:
					cnt += 1
					break
		if cnt == n:
			break
		i += 1
	print(i)
t = 1
for _ in range(t):
	main()