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

D = input().split()



n = N



while True:

    for c in str(n):

        if c in D:

            break

    else:

        break

    n += 1



print(n)
N, K = list(map(int, input().split()))

D = set(input().split())



n = N



while True:

    for c in str(n):

        if c in D:

            break

    else:

        break

    n += 1



print(n)
N, K = list(map(int, input().split()))
D = input().split()
n = N
while True:
    for c in str(n):
        if c in D:
            break
    else:
        break
    n += 1
print(n)

N, K = list(map(int, input().split()))
D = set(input().split())
n = N
while True:
    for c in str(n):
        if c in D:
            break
    else:
        break
    n += 1
print(n)

-D = input().split() +D = set(input().split())