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

S = {int(x) for x in input().split()}

flag = False

while flag == False:

    if N%10 in S:

        N += 1

        continue

    if int((N%100-N%10)/10) in S and N >= 10:

        N += 1

        continue

    if int((N%1000-N%100)/100) in S and N >= 100:

        N += 1

        continue

    if int((N%10000-N%1000)/1000) in S and N >= 1000:

        N += 1

        continue

    if int((N%100000-N%10000)/10000) in S and N >= 10000:

        N += 1

        continue

    flag = True

print(N)

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

S = {int(x) for x in input().split()}

flag = False

while 1:

    if N%10 in S:

        N += 1

        continue

    if int((N%100-N%10)/10) in S and N >= 10:

        N += 1

        continue

    if int((N%1000-N%100)/100) in S and N >= 100:

        N += 1

        continue

    if int((N%10000-N%1000)/1000) in S and N >= 1000:

        N += 1

        continue

    if int((N%100000-N%10000)/10000) in S and N >= 10000:

        N += 1

        continue

    break

print(N)

N, L = list(map(int, input().split()))
S = {int(x) for x in input().split()}
flag = False
while flag == False:
    if N % 10 in S:
        N += 1
        continue
    if int((N % 100 - N % 10) / 10) in S and N >= 10:
        N += 1
        continue
    if int((N % 1000 - N % 100) / 100) in S and N >= 100:
        N += 1
        continue
    if int((N % 10000 - N % 1000) / 1000) in S and N >= 1000:
        N += 1
        continue
    if int((N % 100000 - N % 10000) / 10000) in S and N >= 10000:
        N += 1
        continue
    flag = True
print(N)

N, L = list(map(int, input().split()))
S = {int(x) for x in input().split()}
flag = False
while 1:
    if N % 10 in S:
        N += 1
        continue
    if int((N % 100 - N % 10) / 10) in S and N >= 10:
        N += 1
        continue
    if int((N % 1000 - N % 100) / 100) in S and N >= 100:
        N += 1
        continue
    if int((N % 10000 - N % 1000) / 1000) in S and N >= 1000:
        N += 1
        continue
    if int((N % 100000 - N % 10000) / 10000) in S and N >= 10000:
        N += 1
        continue
    break
print(N)

-while flag == False: +while 1: -    flag = True +    break