import itertools

N,K = list(map(int,input().split()))

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



#使用可能数字の取り出し

Num1 = []

for i in range(10):

  if(i in D): continue

  else: Num1.append(i)

 

#組み合わせ

Comb = []

n = len(str(N))

for i in range(n+1):

  Comb.append(Num1)

Comb = list(itertools.product(*Comb))



ans = -1

for i in range(len(Comb)):

  tmp = Comb[i]

  tmp_num = ''

  for j in range(n): tmp_num += str(tmp[j])

  tmp_num = int(tmp_num)

  if(N <= tmp_num):

    ans = tmp_num

    break

    

if(ans == -1):

  for i in range(len(Comb)):

   tmp = Comb[i]

   tmp_num = ''

   for j in range(n+1): tmp_num += str(tmp[j])

   tmp_num = int(tmp_num)

   if(N <= tmp_num):

     ans = tmp_num

     break

print(ans)
N,K = list(map(int,input().split()))

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



for x in range(N,10**6):

  x = str(x)

  judge = 1

  for xi in x:

    if(xi in D):

      judge = 0 

  if(judge == 1):

    ans = x

    break

    

print(ans)    
import itertools

N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
# 使用可能数字の取り出し
Num1 = []
for i in range(10):
    if i in D:
        continue
    else:
        Num1.append(i)
# 組み合わせ
Comb = []
n = len(str(N))
for i in range(n + 1):
    Comb.append(Num1)
Comb = list(itertools.product(*Comb))
ans = -1
for i in range(len(Comb)):
    tmp = Comb[i]
    tmp_num = ""
    for j in range(n):
        tmp_num += str(tmp[j])
    tmp_num = int(tmp_num)
    if N <= tmp_num:
        ans = tmp_num
        break
if ans == -1:
    for i in range(len(Comb)):
        tmp = Comb[i]
        tmp_num = ""
        for j in range(n + 1):
            tmp_num += str(tmp[j])
        tmp_num = int(tmp_num)
        if N <= tmp_num:
            ans = tmp_num
            break
print(ans)

N, K = list(map(int, input().split()))
D = list(input().split())
for x in range(N, 10**6):
    x = str(x)
    judge = 1
    for xi in x:
        if xi in D:
            judge = 0
    if judge == 1:
        ans = x
        break
print(ans)

-import itertools - -D = list(map(int, input().split())) -# 使用可能数字の取り出し -Num1 = [] -for i in range(10): -    if i in D: -        continue -    else: -        Num1.append(i) -# 組み合わせ -Comb = [] -n = len(str(N)) -for i in range(n + 1): -    Comb.append(Num1) -Comb = list(itertools.product(*Comb)) -ans = -1 -for i in range(len(Comb)): -    tmp = Comb[i] -    tmp_num = "" -    for j in range(n): -        tmp_num += str(tmp[j]) -    tmp_num = int(tmp_num) -    if N <= tmp_num: -        ans = tmp_num +D = list(input().split()) +for x in range(N, 10**6): +    x = str(x) +    judge = 1 +    for xi in x: +        if xi in D: +            judge = 0 +    if judge == 1: +        ans = x -if ans == -1: -    for i in range(len(Comb)): -        tmp = Comb[i] -        tmp_num = "" -        for j in range(n + 1): -            tmp_num += str(tmp[j]) -        tmp_num = int(tmp_num) -        if N <= tmp_num: -            ans = tmp_num -            break