import std.stdio;
import std.conv;
import std.string;

void main() {
	string s;
	while((s = readln()).length != 0) {
		int [] c = [500, 100, 50, 10, 5, 1];
		int res = 0;
		int a;
		a = to!int(chomp(s));
		if(a == 0) break;
		a = 1000 - a;
		for(int i = 0; i < c.length; i++) {
			if(a >= c[i]) {
				res++;
				a -= c[i];
				i--;
			}
		}

		writeln(res);
	}
}