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

l = list(map(int, input().split()))

nums = [i for i in range(10) if not i in l]



while True:

  total = 0

  for num in nums:

    total += list(str(n)).count(str(num))

  

  if total == len(list(str(n))):

    print(n)

    break

  

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

like = list(map(str, set(range(10)) - set(map(int, input().split()))))



while True:

  count = 0

  for c in str(n):

    if c in like:

      count += 1

      

  if count == len(str(n)):

    print(n)

    break

  

  n += 1
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
nums = [i for i in range(10) if not i in l]
while True:
    total = 0
    for num in nums:
        total += list(str(n)).count(str(num))
    if total == len(list(str(n))):
        print(n)
        break
    n += 1

n, k = list(map(int, input().split()))
like = list(map(str, set(range(10)) - set(map(int, input().split()))))
while True:
    count = 0
    for c in str(n):
        if c in like:
            count += 1
    if count == len(str(n)):
        print(n)
        break
    n += 1

-l = list(map(int, input().split())) -nums = [i for i in range(10) if not i in l] +like = list(map(str, set(range(10)) - set(map(int, input().split())))) -    total = 0 -    for num in nums: -        total += list(str(n)).count(str(num)) -    if total == len(list(str(n))): +    count = 0 +    for c in str(n): +        if c in like: +            count += 1 +    if count == len(str(n)):