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

aux = []
for x in a:
    for y in b:
        aux.append(int(''.join(sorted(set('%s%s' % (x, y))))))

print(sorted(aux)[0])
