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

D = list(map(str,input().split()))

x = n

while True:

  y = str(x)

  for i in range(len(y)):

    if y[i] in D:

      break

  else:

    ans = x

    break

  x += 1

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

D = list(map(str,input().split()))

x = n

while True:

  y = str(x)

  for i in y:

    if i in D:

      break

  else:

    ans = x

    break

  x += 1

print(ans)
n, k = list(map(int, input().split()))
D = list(map(str, input().split()))
x = n
while True:
    y = str(x)
    for i in range(len(y)):
        if y[i] in D:
            break
    else:
        ans = x
        break
    x += 1
print(ans)

n, k = list(map(int, input().split()))
D = list(map(str, input().split()))
x = n
while True:
    y = str(x)
    for i in y:
        if i in D:
            break
    else:
        ans = x
        break
    x += 1
print(ans)

-    for i in range(len(y)): -        if y[i] in D: +    for i in y: +        if i in D: