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

D = list(input().split())

while True:

	if any (d in str(n) for d in D):

		n += 1

	else:

		print(n)

		exit()

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

d = set(eval(input()))

while len(set(str(n))&d):

	n += 1

print(n)
n, k = list(map(int, input().split()))
D = list(input().split())
while True:
    if any(d in str(n) for d in D):
        n += 1
    else:
        print(n)
        exit()

n, k = list(map(int, input().split()))
d = set(eval(input()))
while len(set(str(n)) & d):
    n += 1
print(n)

-D = list(input().split()) -while True: -    if any(d in str(n) for d in D): -        n += 1 -    else: -        print(n) -        exit() +d = set(eval(input())) +while len(set(str(n)) & d): +    n += 1 +print(n)