program payment
  implicit none
  integer :: m, p(0:100001) = 0, r(0:100001) = 0, i, x = 1e9
  character(100002) :: n
  read(*,*) n
  m = len_trim(n)
  do i = 1, m
    p(m-i) = ichar(n(i:i))-48
  end do
  do i = 0, 10**m
    x = min(x,sum(p)+sum(r))
    call inc(p)
    call inc(r)
  end do
  write(*,'(i0)') x
contains
  subroutine inc(a)
    integer, intent(inout) :: a(0:100001)
    integer :: i
    a(0) = a(0)+1
    i = 0
    do while (a(i) > 9)
      a(i) = 0
      i = i+1
      a(i) = a(i)+1
    end do
  end
end program payment