n, m = map(int, input().split())

a = set(sorted(map(int, input().split())))
b = set(sorted(map(int, input().split())))

res = set()

for i in a:
    for j in b:
        k = (i, j)

        nums = set()
        nums.add(str(k[0]))
        nums.add(str(k[1]))

        nums = list(sorted(nums))

        res.add(abs(int(''.join(nums))))


#print(res)

print(min(res))