import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;

void main()
{
	int n;
	auto coin = [500, 100, 50, 10, 5, 1];
	while (n = readln.chomp.to!int, n) {
		auto cng = 1000 - n;
		int cnt;
		foreach (c; coin) {
			cnt += cng / c;
			cng %= c;
		}
		cnt.writeln;
	}
}