import scala.math.{min,max}

object Main {
  val sc = new java.util.Scanner(System.in)
  def solve(c: Int): Int = c match {
    case 0 => 0
    case 1 => 1
    case 5 => 1
    case 10 => 1
    case 50 => 1
    case 100 => 1
    case 500 => 1
    case n if n > 500 => 1 + solve(c-500)
    case n if n > 100 => 1 + solve(c-100)
    case n if n > 50 => 1 + solve(c-50)
    case n if n > 10 => 1 + solve(c-10)
    case n if n > 5 => 1 + solve(c-5)
    case n if n > 1 => 1 + solve(c-1)
  }

  def main(args: Array[String]) {
    while(true) {
      val n = sc.nextInt
      if(n == 0) return
      val c = 1000-n
      println(solve(c))
    }
  }
}