Num = [int(n) for n in input().rstrip().split()]



D = [int(n) for n in input().rstrip().split()]



for i in range(Num[0],100000):

    if len(set([str(n) for n in D]) & set(str(i))) == 0:

        print((int(i)))

        break

    



Num = [int(n) for n in input().rstrip().split()]



D = [int(n) for n in input().rstrip().split()]



# numbers = [ n for n in range(10) if not n in D]



flag = 0



for i in range(Num[0],10*Num[0]+1):

    for j in str(i):

        if int(j) in D:

            flag = 1

            break



    if flag == 0:

        print(i)

        break

    else:

        flag =0 

        
Num = [int(n) for n in input().rstrip().split()]
D = [int(n) for n in input().rstrip().split()]
for i in range(Num[0], 100000):
    if len(set([str(n) for n in D]) & set(str(i))) == 0:
        print((int(i)))
        break

Num = [int(n) for n in input().rstrip().split()]
D = [int(n) for n in input().rstrip().split()]
# numbers = [ n for n in range(10) if not n in D]
flag = 0
for i in range(Num[0], 10 * Num[0] + 1):
    for j in str(i):
        if int(j) in D:
            flag = 1
            break
    if flag == 0:
        print(i)
        break
    else:
        flag = 0

-for i in range(Num[0], 100000): -    if len(set([str(n) for n in D]) & set(str(i))) == 0: -        print((int(i))) +# numbers = [ n for n in range(10) if not n in D] +flag = 0 +for i in range(Num[0], 10 * Num[0] + 1): +    for j in str(i): +        if int(j) in D: +            flag = 1 +            break +    if flag == 0: +        print(i) +    else: +        flag = 0