import util.control.Breaks._

object Main extends App {
  breakable(
    while (true) {
      val amount = io.StdIn.readInt()
      if (amount == 0) break()
      var change = 1000 - amount
      var count = 0
      val coins = Array(500,100,50,10,5,1)
      coins foreach {c => while (change >= c) {change -= c; count += 1}}
      println(count)
    }
  )
}
