program prob4
    implicit none
    integer(16)::N, M
    integer(16)::i,j,ans,com
    integer::v(17)
    read(*,*) N

    v = 0
    M = N
    do i = 16, 0, -1
        if(N >= 10**i) then
            v(i+1) = N/(10**i)
            N = N - N/(10**i) * 10**i
        end if
    end do

    if(M < 10) then
        write(*,*) M
    else
        do i = 17, 1, -1
            if(v(i) > 0)then
                j = i
                exit
            end if
        end do

        if(v(j) == 1)then
            write(*,*) 9*(j-1)
        else
            ans = v(j) - 1 + 9*(j-1)
            do i = 1,j-1
                if(v(i) < 9) then
                    exit
                else if(i == j-1)then
                    ans = ans + 1
                end if
            end do
            write(*,*) ans      
        end if
    end if

    stop
end program