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

li1 = set(map(int,input().split()))

li2 = {1,2,3,4,5,6,7,8,9,0}

nu = list(li1^li2)



i = 1

while True:

    aa = list(str(a))

    aa = [int(x) for x in aa]

    if set(aa).issubset(set(nu)):

        print(a)

        break

    a += i
n,k = list(map(int,input().split()))

L = list(map(int,input().split()))

num = [1,2,3,4,5,6,7,8,9,0]

li = list(set(num).difference(set(L)))



flag = False

while True:

    n = str(n)

    for i in range(len(n)):

        if int(n[i]) in li:

            flag = True

        else:

            flag= False

            break

    if flag:

        print(n)

        exit()

    n = int(n)

    n +=1
a, b = list(map(int, input().split()))
li1 = set(map(int, input().split()))
li2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
nu = list(li1 ^ li2)
i = 1
while True:
    aa = list(str(a))
    aa = [int(x) for x in aa]
    if set(aa).issubset(set(nu)):
        print(a)
        break
    a += i

n, k = list(map(int, input().split()))
L = list(map(int, input().split()))
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
li = list(set(num).difference(set(L)))
flag = False
while True:
    n = str(n)
    for i in range(len(n)):
        if int(n[i]) in li:
            flag = True
        else:
            flag = False
            break
    if flag:
        print(n)
        exit()
    n = int(n)
    n += 1

-a, b = list(map(int, input().split())) -li1 = set(map(int, input().split())) -li2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0} -nu = list(li1 ^ li2) -i = 1 +n, k = list(map(int, input().split())) +L = list(map(int, input().split())) +num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] +li = list(set(num).difference(set(L))) +flag = False -    aa = list(str(a)) -    aa = [int(x) for x in aa] -    if set(aa).issubset(set(nu)): -        print(a) -        break -    a += i +    n = str(n) +    for i in range(len(n)): +        if int(n[i]) in li: +            flag = True +        else: +            flag = False +            break +    if flag: +        print(n) +        exit() +    n = int(n) +    n += 1