program digit
	implicit none
    character(len=17) :: n
    character(len=17),parameter :: q = '99999999999999999'
    integer :: j
    integer :: i, ans
    
    read(*,*) n
    read(n(1:1),*) j !最高位の数字
    
    i = len_trim(n) !桁数

    if ( i==1 ) then
    	write(*,*) n
        stop
    end if
    
    if ( n(2:i) == q(2:i) ) then
    	ans = 9*(i-1) + j        
        write (*,*) ans
        stop
    end if
    
    ans = 9*(i-1) + j - 1
    write(*,*) ans
    stop
end program digit