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

d = {str(x) for x in input().split()}

a = {int(x) for x in range(10)}

l = a - d



for i in range(n,100000+1):

    i2 = str(i)

    for j in range(len(i2))[::-1]:

        if i2[j] in d:

            break

    else:

        print(i2)

        break
n, k = list(map(int, input().split()))

d = {str(x) for x in input().split()}

a = {str(x) for x in range(10)}

l = a - d



while True:

    if all(j in l for j in str(n)):

        print(n)

        break

    else:

        n += 1
n, k = list(map(int, input().split()))
d = {str(x) for x in input().split()}
a = {int(x) for x in range(10)}
l = a - d
for i in range(n, 100000 + 1):
    i2 = str(i)
    for j in range(len(i2))[::-1]:
        if i2[j] in d:
            break
    else:
        print(i2)
        break

n, k = list(map(int, input().split()))
d = {str(x) for x in input().split()}
a = {str(x) for x in range(10)}
l = a - d
while True:
    if all(j in l for j in str(n)):
        print(n)
        break
    else:
        n += 1

-a = {int(x) for x in range(10)} +a = {str(x) for x in range(10)} -for i in range(n, 100000 + 1): -    i2 = str(i) -    for j in range(len(i2))[::-1]: -        if i2[j] in d: -            break +while True: +    if all(j in l for j in str(n)): +        print(n) +        break -        print(i2) -        break +        n += 1