program answer
  implicit none

  integer(8) :: N, i, ans, k
  integer, allocatable :: p(:)
  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

   if(k==1) then
      write(*,*) N
      stop
   end if

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

   allocate(p(k))
   do i = 1, k-1
      p(i)=mod(N,10)
      N=(N-p(i))/10
   end do
   p(N)=N

   if(sum(p)>ans) then
      ans = sum(p)
   end if
   

   write(*,*) ans
   deallocate(p)
   stop
  end program answer
