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

loop do
  n = gets.to_i
  break if n == 0
  m = 1000 - n
  cnt = 0
  coins.each do |coin|
    while m - coin >= 0
      m -= coin
      cnt += 1
    end
  end
  puts cnt
end