#!/usr/bin/ruby                                                                                                                                   
# -*- coding: utf-8 -*-                                                                                                                           

while (change = 1000 - gets.to_i()) != 1000
  res = 0
  coins = [500, 100, 50, 10, 5, 1]
  coins.each {|x|
    res += change / x
    change %= x
  }
  puts res
end