def main():
    mn = list((map(int,input().split())))
    n = mn[0]
    m = mn[1]
    result = []
    numn = input().split()
    numm = input().split()
    min1 = 100000000000
    min2 = 100000000000

    for i in range(n):
        if numn[i] in numm:
            result.append(int(numn[i]))

    for i in range(n):

        for j in range(m):
            if int(numn[i]+numm[j])<min1:
                min1 = int(numn[i]+numm[j])

    for i in range(m):
        for j in range(n):
            if int(numm[i]+numn[j])<min2:
                min2 = int(numm[i]+numn[j])
    min3 = 1000000000
    if len(result)!=0:
        min3 = min(result)




    result2 = []
    result2.append(min1)
    result2.append(min2)
    result2.append(min3)
    print(min(result2))

main()