program sample
  implicit none
  integer(16) :: i,j,x,y,count,N
  integer(16) :: a(40)
    
  read(*,*) N

  x=N

  count=0

  do i=1,16
     if (x/10>1) then
        x=x/10
        count = i
     end if
  end do

  if (count >0) then
     if (10**count-1 <= N .and. N<=2*10**count-2) then
        y=9*count
     else if (2*10**count-1 <= N .and. N<=3*10**count-2) then
        y=9*count+1
     else if (3*10**count-1 <= N .and. N<=4*10**count-2) then
        y=9*count+2
     else if (4*10**count-1 <= N .and. N<=5*10**count-2) then
        y=9*count+3
     else if (5*10**count-1 <= N .and. N<=6*10**count-2) then
        y=9*count+4
     else if (6*10**count-1 <= N .and. N<=7*10**count-2) then
        y=9*count+5
     else if (7*10**count-1 <= N .and. N<=8*10**count-2) then
        y=9*count+6
     else if (8*10**count-1 <= N .and. N<=9*10**count-2) then
        y=9*count+7
     else if (9*10**count-1 <= N .and. N<=10**count-2) then
        y=9*count+8
     else if (10*10**count-1 == N .and. N<=10**count-2) then
        y=9*count+9
     end if
  else
     y=N
  end if
     
  write(*,*) y
  
  stop
end program sample
