import math


def mergeSort(lst):
    if len(lst) <= 1:
        return lst
    mid = math.floor(len(lst) / 2)
    left = mergeSort(lst[0:mid])
    right = mergeSort(lst[mid:])
    return merge(left, right)


def merge(arr1, arr2):
    i = 0
    j = 0
    lst = []
    while i < len(arr1) and j < len(arr2):
        if arr1[i] < arr2[j]:
            lst.append(arr1[i])
            i += 1
        else:
            lst.append(arr2[j])
            j += 1

    while i < len(arr1):
        lst.append(arr1[i])
        i += 1
    while j < len(arr2):
        lst.append(arr2[j])
        j += 1

    return lst


n, m=input().split()
n = int(n)
m = int(m)
lst1 = list(map(int, input().split()))
lst2 = list(map(int, input().split()))
lst1 = mergeSort(lst1)
lst2 = mergeSort(lst2)
i=0
isPresent= False
while i < len(lst2):
    if lst2[i] in lst1:
        print(lst2[i])
        isPresent=True
        break
    i+=1
if not isPresent:
    str1=str(lst1[0])
    str2=str(lst2[0])
    first=int(str1+str2)
    second=int(str2+str1)
    print(first if first<second else second)