program example
    implicit none

    integer(8) ::  K,b,i=1,j
    integer(8),allocatable :: a(:)

    read(*,*) K

    a=(/1,2,3,4,5,6,7,8,9/)

    if(K<=9) then

        write(*,*) a(K)
        goto 2

    end if

    do
        
        do j=-1,1,1
            b=mod(a(i),10)+j
            if (b<0 .or. b>9) then
                goto 3
            end if
            a=[a,a(i)*10+b]

            if(size(a)==K) then
                goto 1
            end if

        3 end do

        i=i+1

    end do

1    write(*,*) a(K)

2 end program example
