program payment
  implicit none
  character(1000001) :: s
  integer :: a(0:1000001) = 0, n, i, j, x = 0
  read(*,*) s
  n = len_trim(s)
  do i = 1, n
    a(n-i) = ichar(s(i:i))-48
  end do
  do i = 0, n-1
    if (a(i) > 5 .or. (a(i) == 5 .and. a(i+1) >= 5)) then
      x = x+10-a(i)
      j = i+1
      do while (a(j) == 9)
        a(j) = 0
        j = j+1
      end do
      a(j) = a(j)+1
    else
      x = x+a(i)
    end if
  end do
  write(*,'(i0)') x
end program payment