n,_,*d=open(0).read().split()

n=int(n)

while 1:

 if all(d not in str(n)for d in d):exit(print(n))

 n+=1

n=int(input().split()[0])

a=set(eval(input()))

while a&set(str(n)):n+=1

print(n)
n, _, *d = open(0).read().split()
n = int(n)
while 1:
    if all(d not in str(n) for d in d):
        exit(print(n))
    n += 1

n = int(input().split()[0])
a = set(eval(input()))
while a & set(str(n)):
    n += 1
print(n)

-n, _, *d = open(0).read().split() -n = int(n) -while 1: -    if all(d not in str(n) for d in d): -        exit(print(n)) +n = int(input().split()[0]) +a = set(eval(input())) +while a & set(str(n)): +print(n)