PROGRAM ATCODER

implicit none
integer :: n, k, cost, i, j, check
integer,allocatable :: d(:)

read(*,*) n, k
allocate (d(k))
read(*,*) (d(i), i=1,k)

cost = n

do
	check = 0
	do j = 1, k
		if(cost/1000 == d(j))then
			cost = (cost/1000)*1000 + 1000
       		check = 1
		end if
		if(mod(cost,1000)/100 == d(j))then
            cost = (cost/100)*100 + 100
	        check = 1
		end if
		if(mod(cost,100)/10 == d(j))then
			cost = (cost/10)*10 + 10
	        check = 1
		end if
		if(mod(cost,10) == d(j))then
			cost = cost + 1
	        check = 1
		end if
	end do
    if (check == 0) then
		write(*,'(i0)') cost
		stop
	end if
end do

END PROGRAM ATCODER