import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;

void main(){
    int[] change;
    while(true){
        int input = readln().chomp().to!int();
        if(input != 0){ change ~= 1000 - input; }else{ break; }
    }
    foreach(int i; change){
        int count = 0;
        while(i - 500 >= 0){
            i -= 500; count++;
        }
        while(i - 100 >= 0){
            i -= 100; count++;
        }
        while(i - 50 >= 0){
            i -= 50; count++;
        }
        while(i - 10 >= 0){
            i -= 10; count++;
        }
        while(i - 5 >= 0){
            i -= 5; count++;
        }
        while(i - 1 >= 0){
            i -= 1; count++;
        }
        writeln(count);
    }
}