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

an = []

for i in a:
	if i in b:
		an.append(i)

for i in a:
	for j in b:
		an.append(int(str(i) + str(j)))
		an.append(int(str(j) + str(i)))
print(min(an))
