program abc042_a
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: k,i,j,tn,dd(0:10)
    integer(int32),allocatable:: d(:)
    character(5):: cn
    logical:: hate, hnum(0:9)

    read*, cn, k
    allocate(d(k))
    read*, d(:)
    hnum(:) = .false.
    do i=1,k
        hnum(d(i)) = .true.
    end do
    dd(10) = 10
    do i=9,0,-1
        if (hnum(i)) then
            dd(i) = dd(i+1)
        else
            dd(i) = i 
        end if
    end do

    cn = adjustr(cn)
    
    do i=5,1,-1
        if (cn(i:i) == ' ') cycle
        hate = .false.
        tn = ichar(cn(i:i)) - ichar('0')
        do j=1,k
            if (dd(tn) < 10) then
                cn(i:i) = char(dd(tn)+ichar('0'))
            else
                call kuriage(i-1)
                cn(i:i) = char(dd(0)+ichar('0'))
            end if
        end do
    end do
    print'(a)', trim(adjustl(cn))
contains
    recursive subroutine kuriage(k)
        integer(int32):: k
        
        if (cn(k:k) == ' ') then
            cn(k:k) = '1'
        else if (cn(k:k) == '9') then
            cn(k:k) = '0'
            call kuriage(k-1)
        else
            cn(k:k) = char(ichar(cn(k:k))+1)
        end if
    end subroutine

end program abc042_a