n,k,*a=open(0).read().split();n=int(n)

while any(s in set(a)for s in str(n)):n+=1

print(n)
n,_=input().split();s=set(list(eval(input())));n=int(n);print((n+[len(s&set(str(b)))for b in range(n,99999)].index(0)))
n, k, *a = open(0).read().split()
n = int(n)
while any(s in set(a) for s in str(n)):
    n += 1
print(n)

n, _ = input().split()
s = set(list(eval(input())))
n = int(n)
print((n + [len(s & set(str(b))) for b in range(n, 99999)].index(0)))

-n, k, *a = open(0).read().split() +n, _ = input().split() +s = set(list(eval(input()))) -while any(s in set(a) for s in str(n)): -    n += 1 -print(n) +print((n + [len(s & set(str(b))) for b in range(n, 99999)].index(0)))