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

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



n = N



while True:

    flg = False

    for Di in D:

        if str(Di) in str(n):

            flg = True

    if not flg:

        break

    n += 1



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

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



n = N



while True:

    i = n

    while i != 0:

        if i % 10 in D:

            break

        i = i // 10

    if i == 0:

        break

    n += 1



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

N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
n = N
while True:
    i = n
    while i != 0:
        if i % 10 in D:
            break
        i = i // 10
    if i == 0:
        break
    n += 1
print(n)

-    flg = False -    for Di in D: -        if str(Di) in str(n): -            flg = True -    if not flg: +    i = n +    while i != 0: +        if i % 10 in D: +            break +        i = i // 10 +    if i == 0: