inputs = STDIN.map(&:chomp).map(&:to_i)

$coins = []

def calc_cost(input)
  (1000 - input)
end

def refund(cost)
  [500, 100, 50, 10, 5, 1].each { | coin |
    if cost >= coin
      $coins << coin
      cost -= coin
      return refund(cost)
    end
  }
  if cost == 0
   ans = $coins.count
   $coins = []
   return ans
  end
  'something wrong'
end

inputs.each { |input|
  puts refund(1000 - input.to_i) if input > 0
}
