def GetCommon(first,second):
    first.sort()
    for i in first:
        if i in second:
            return i
        
    return -1

def PrettyIntegers():
    n,m = map(int,raw_input().split())
    first = list(map(int,raw_input().split()))
    second = list(map(int,raw_input().split()))
    common = GetCommon(first,second)
    if common != -1:
        print common
    else:
        i1 = int(str(min(first))+str(min(second)))
        i2 = int(str(min(second))+str(min(first)))
        print min(i1,i2)
    
PrettyIntegers()