program answer
  implicit none

  integer(8) :: N, i, ans, k
  read(*,*) N

  if(N<10) then
     k = 1
  else 
     do i = 1, N
       if(N>10**(i).and.N<=10**(i+1)) then
          k=i+1
          exit
        end if
      end do
   end if


   ans = int(N/(10**(k-1)))-1+9*(k-1)

   write(*,*) ans
   stop
  end program answer