
object Main extends App {
  import scala.io.StdIn.{readLine}
  def change(price:Int, coins:List[Int] = List(500,100,50,10,5,1)):Int = {
    coins match{
      case c::t  ⇒ price / c + change(price % c, t)
      case Nil ⇒ 0
    }
  }
  var price = readLine().toInt
  while (price != 0){
    println(change(1000 - price))
    price = readLine().toInt
  }
}
