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


