program payment
    implicit none
    integer(4)::i,digits,next_digits,move_up=0, count=0
    character(100000000):: s

    read*, s

    do i=len_trim(s),1,-1
        digits = ichar(s(i:i))-48
        next_digits = ichar(s(i-1:i-1))-48
        if ( digits+move_up  <= 5) then
            if (digits+move_up == 5 .and. next_digits >= 5) then
                count=count+10-(digits+move_up)
                move_up=1
            else
                count=count+digits+move_up
                move_up=0
            end if
        else
            count=count+10-(digits+move_up)
            move_up=1
        end if
        ! print*, digits, count
    end do

    print*,count+move_up

    
end program payment