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

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



while len(set(str(N))&D)>0:

  N+=1



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

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



for n in range(N,100000):

  if len(set(str(n))&D)==0:

    print(n)

    break
N, K = list(map(int, input().split()))
D = set(input().split())
while len(set(str(N)) & D) > 0:
    N += 1
print(N)

N, K = list(map(int, input().split()))
D = set(input().split())
for n in range(N, 100000):
    if len(set(str(n)) & D) == 0:
        print(n)
        break

-while len(set(str(N)) & D) > 0: -    N += 1 -print(N) +for n in range(N, 100000): +    if len(set(str(n)) & D) == 0: +        print(n) +        break