money = [500, 100, 50, 10, 5, 1]

while payment = gets.chomp.to_i
  count = 0
  break if payment == 0
  change = 1000 - payment
  money.each do |val|
    count += change / val
    change %= val
  end
  puts count
end

