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

A = list(input().split())

for i in range(10 * n):

    for a in A:

        if a in str(n + i):

            break

    else:

        ans = n + i

        break

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

A = list(input().split())



for i in range(n * 10):

    for j in str(n + i):

        if j in A:

            break

    else:

        print((n + i))

        break
n, k = list(map(int, input().split()))
A = list(input().split())
for i in range(10 * n):
    for a in A:
        if a in str(n + i):
            break
    else:
        ans = n + i
        break
print(ans)

n, k = list(map(int, input().split()))
A = list(input().split())
for i in range(n * 10):
    for j in str(n + i):
        if j in A:
            break
    else:
        print((n + i))
        break

-for i in range(10 * n): -    for a in A: -        if a in str(n + i): +for i in range(n * 10): +    for j in str(n + i): +        if j in A: -        ans = n + i +        print((n + i)) -print(ans)