program main
	implicit none
    integer(16)::x,b,keta
    integer::i
    read(*,*)x
    b=x
    keta=0_16
    do while(b>0)
    	b=b/10_16
        keta=keta+1
    end do
    !write(*,*) keta
    if(x==1)then
        write(*,*) 1
    else if(x/10**(keta-1) ==1)then
    	write(*,*) 9*(keta-1)
    else if(x+1==10**(keta))then
    	write(*,*) 9*(keta)
    else
    	write(*,*) 9*(keta-1)+x/10**(keta-1) -1
    end if
    !write(*,*) x/(10_16**(keta-1_16))
    stop
end program main
