#encoding:utf-8 No.0521 2014.11.11
while (price = gets.chomp.to_i) != 0
  change = 1000 - price
  count = 0
  coins = [500,100,50,10,5,1]
  coins.each do |coin|
    count += change/coin
    change = change%coin
  end
  puts count
end