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

d = input().split()





f = True

while(f):

  for s in d:

    if s in str(n):

      _ = n

      n += 1

      break

  else:

    break

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

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



while(True):

    x = n

    while(x):

        if x%10 in d:

            break

        x//=10

    else:

        print(n)

        quit(0)

    n += 1
n, k = list(map(int, input().split()))
d = input().split()
f = True
while f:
    for s in d:
        if s in str(n):
            _ = n
            n += 1
            break
    else:
        break
print(n)

n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
while True:
    x = n
    while x:
        if x % 10 in d:
            break
        x //= 10
    else:
        print(n)
        quit(0)
    n += 1

-d = input().split() -f = True -while f: -    for s in d: -        if s in str(n): -            _ = n -            n += 1 +d = list(map(int, input().split())) +while True: +    x = n +    while x: +        if x % 10 in d: +        x //= 10 -        break -print(n) +        print(n) +        quit(0) +    n += 1