while true do
	kaimono = gets.to_i
	if kaimono == 0 then
		break		
	end

	otsuri = 1000 - kaimono

	kozeni500 = otsuri / 500
	otsuri = otsuri - 500 * kozeni500

	kozeni100 = otsuri / 100
	otsuri = otsuri - 100 * kozeni100

	kozeni50 = otsuri / 50
	otsuri = otsuri - 50 * kozeni50

	kozeni10 = otsuri / 10
	otsuri = otsuri - 10 * kozeni10

	kozeni5 = otsuri / 5
	otsuri = otsuri - 5 * kozeni5

	kozeni1 = otsuri / 1

	puts(kozeni1 + kozeni5 + kozeni10 + kozeni50 + kozeni100 + kozeni500)	
end